#爬取csdn博客首页
import urllib.request
import re
url="https://blog.csdn.net/"
#浏览器伪装
headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
#安装为全局
urllib.request.install_opener(opener)
data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
pat='<a href="(.*?)" target="_blank" data-report-click'
A=re.compile(pat).findall(data)
for i in range(0,len(A)):
localpath="E:\\daima\\"+str(i)+".html"
B=A[i]
urllib.request.urlretrieve(B,filename=localpath)
print("当前文章"+str(i)+"爬取成功")
Python-爬取csdn博客首页
最新推荐文章于 2021-07-25 17:51:22 发布