python多线程爬取_python 多线程方法爬取微信公众号文章
'''
fh=open("/home/urllib/test/1.html","wb")
fh.write(html1.encode("utf-8"))
fh.close()
#再次以追加写入的方式打开文件,以写入对应文章内容
fh=open("/home/urllib/test/1.html","ab")
i=1
while(True):
try:
url=self.urlqueue.get()
data=urllib.request.urlopen(url).read().decode('utf-8')
data=str(data)
titlepat='var msg_title = "(.*?)";'
contentpat='id="js_content">(.*?)id="js_sg_bar"'
title=re.compile(titlepat).findall(data)
content=re.compile(contentpat,re.S).findall(data)
#初始化标题与内容
thistitle = "此次没有获取到"
thiscontent= "此次没有获取到"
#如果标题列表不为空,说明找到了标题,取列表第0个元素,即此次标题赋给变量thistitle
if (title!=[]):
thistitle = title[0]
if (content!=[]):
thiscontent = content[0]
#将标题与内容汇总赋给变量dataall
dataall = "
标题为:"+thistitle+"
内容为:"+thiscontent+"
"
fh.write(dataall.encode('utf-8'))
print("第"+str(i)+"个网页处理")
time.sleep(1)
i+=1
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
time.sleep(10)
except Exception as e:
print("exception"+str(e))
time.sleep(1)
fh.close()
html2='''
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
