Python-爬取csdn博客首页

#爬取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=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)