终于,我还是对自己的博客下手了
爬取自己博客曾经发布过的所有标题
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
本文来自博客园,作者:Hany47315,转载请注明原文链接:https://www.cnblogs.com/hany-postq473111315/p/13087385.html