爬取糗事百科的Python脚本 发表于 2017-10-26 | 分类于 python 12345678910111213141516#糗事百科import urllib.requestimport reheaders = ("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("----------------")