标签:内容 pen 标题 https 结束 size port 自己 code
爬取自己博客曾经发布过的所有标题
import csv
import requests
from bs4 import BeautifulSoup
# https://www.cnblogs.com/hany-postq473111315/
# https://www.cnblogs.com/hany-postq473111315/default.html?page=2
for num in range(1,44):
url = ‘https://www.cnblogs.com/hany-postq473111315/default.html?page=‘ + str(num)
response = requests.get(url)
response.raise_for_status()
response.encoding = response.apparent_encoding
html = response.text
soup = BeautifulSoup(html, "html.parser")
try:
for i in range(50):
# print(soup.find_all(‘a‘,attrs={‘class‘:‘postTitle2‘})[i].text.strip(‘\n‘).strip())
with open("博客园标题.txt", "a+") as f:
f.write(soup.find_all(‘a‘,attrs={‘class‘:‘postTitle2‘})[i].text.strip(‘\n‘).strip() + ‘\n‘)
# 向文件写入内容
print("爬取结束,并存入文件")
except:
pass
2020-06-10
标签:内容 pen 标题 https 结束 size port 自己 code
原文地址:https://www.cnblogs.com/hany-postq473111315/p/13087385.html