python-糗事百科段子爬虫

#糗事百科段子爬虫
import urllib.request
import re
#浏览器伪装
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"
A=urllib.request.build_opener()
A.addheaders=[headers]
#安装为全局
urllib.request.install_opener(A)
for i in range(0,3):
    thisurl="https://www.qiushibaike.com/text/page/"+str(i+1)+"/"
    data=urllib.request.urlopen(thisurl).read().decode("utf-8","ignore")
    pat='
.*?(.*?).*?
'
resoult=re.compile(pat,re.S).findall(data) for j in range(0,len(resoult)): print(resoult[j]) print("___________________")

你可能感兴趣的:(python)