#用户代理池概述
#用户代理池,即将不同的用户代理组建成为一个池子
#用户代理池的构建
import urllib.request
import re
import random
url="https://www.csdn.net/"
uapools=[
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36",
]
def ua(uapools):
#实现随机
thisua=random.choice(uapools)
print(thisua)
headers=("User-Agent",thisua)
opener=urllib.request.build_opener()
opener.addheaders=[headers]
#安装为全局
urllib.request.install_opener(opener)
data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
pat=' <div class="title">\s*?<h2>\s*?<a href="(.*?)"'
rst=re.compile(pat).findall(data)#拿到首页所有的博文链接
#print(rst)
for i in range(0,len(rst)):
ua(uapools)
#因为要与字符串连接,所以先转化为字符串
localpath="D:\\pythonprojects\\result\\"+str(i)+".html"
thislink=rst[i]
#一键下载
urllib.request.urlretrieve(thislink,filename=localpath)
print("当前文章(第"+str(i)+"篇)爬取成功!")
`