#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
原文地址:http://www.cnblogs.com/rabbittail/p/7631942.html