爬取自己博客曾经发布过的所有标题
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('
').strip())
with open("博客园标题.txt", "a+") as f:
f.write(soup.find_all('a',attrs={'class':'postTitle2'})[i].text.strip('
').strip() + '
')
# 向文件写入内容
print("爬取结束,并存入文件")
except:
pass
2020-06-10