web crawling(plus6) more than one line

Posted 兔子的尾巴_Mini

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web crawling(plus6) more than one line相关的知识,希望对你有一定的参考价值。

#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的主要内容,如果未能解决你的问题,请参考以下文章

web crawling(plus2) comment crawling

web crawling(plus5) crawling wechat

web crawling(plus5) news crawling and proxy

web crawling

web crawling(plus1)

web crawling(plus10)scrapy 4