作者:jerry613 | 来源:互联网 | 2023-10-12 09:54
本文由编程笔记#小编为大家整理,主要介绍了终于,我还是对自己的博客下手了相关的知识,希望对你有一定的参考价值。
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