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='''
總結
以上是生活随笔為你收集整理的python多线程爬取_python 多线程方法爬取微信公众号文章的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python列表的实现原理_python
- 下一篇: 钱站审批查征信吗