码迷,mamicode.com
首页 > Web开发 > 详细

web crawling(plus6) more than one line

时间:2017-10-06 19:03:24      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:bin   attr   urlopen   cep   code   pre   /usr   win   head   

#Author:Mini
#!/usr/bin/env python
import urllib.request
import re
import urllib.error
import threading
headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
urllib.request.install_opener(opener)
try:
class A(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
for i in range (1,10,2):
print("line A!")
url= "https://www.qiushibaike.com/hot/page/" + str(i) + "/"
pagedata = urllib.request.urlopen(url).read().decode("utf-8","ignore")
pat = ‘<div class="content"><span>(.*?)</span>‘
datalist = re.compile(pat, re.S).findall(pagedata)
for j in range(0, len(datalist)):
print(str(i) + "." + str(j) + "content:")
print(datalist[j])
class B (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
for i in range(0,10,2):
print("line B!")
url="https://www.qiushibaike.com/hot/page/" + str(i) + "/"
pagedata = urllib.request.urlopen(url)
pagedata=pagedata.read().decode("utf-8","ignore")
pat = ‘<div class="content"><span>(.*?)</span>‘
datalist = re.compile(pat, re.S).findall(pagedata)
for j in range(0, len(datalist)):
print(str(i) + "." + str(j) + "content:")
print(datalist[j])
t1=A()
t1.start()
t2=B()
t2.start()
except urllib.error.URLError as e:
if hasattr(e, "code"):
print(e.code)
if hasattr(e, "reason"):
print(e.reason)




web crawling(plus6) more than one line

标签:bin   attr   urlopen   cep   code   pre   /usr   win   head   

原文地址:http://www.cnblogs.com/rabbittail/p/7631942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!