爬取糗事百科的Python脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#糗事百科
import urllib.request
import re
headers = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
for k in range(0,10):
url = "http://www.qiushibaike.com/8hr/page/"+ str(k)
data = opener.open(url).read().decode('utf-8','ignore')
pat = '<div class="content">\s*?<span>(.*?)</span>.*?</div>'
rst = re.compile(pat,re.S).findall(data)
for i in range(0,len(rst)):
print(rst[i])
print("----------------")