|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我在爬取腾讯视频评论时遇到这种情况,求大佬指点,以下是报错的码,最下面是我写的代码
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1317, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1016, in _send_output
self.send(msg)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 956, in send
self.connect()
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 928, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\豆瓣影评\海王尝试.py", line 15, in <module>
data = urllib.request.urlopen(url).read().decode()
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 543, in _open
'_open', req)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>
我的代码:
# -*- coding: utf-8 -*-
import urllib.request
import re
import urllib.error
import sys
#import emoji
headers = ("User-Agent","Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0")
openr = urllib.request.build_opener()
urllib.request.install_opener(openr)
#url="https://video.coral.qq.com/varticle/2904111363/comment/v2?callback=_varticle2904111363commentv2&orinum=10&oriorder=o&pageflag=1&cursor=6455103461504421579"
url="http://coral.qq.com/article/2671700896/comment/v2?callback=_article2671700896commentv2&orinum=10&oriorder=o&pageflag=1&cursor=6464414196067450754"
for i in range(0,1200):
data = urllib.request.urlopen(url).read().decode()
#emoji.demojize(str)
#highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
#nickname = highpoints.sub(u'',data)
patcom = '"content":"(.*?)",'
patcom1 = '"last":"(.*?)",'
comdata1 = re.compile(patcom1).findall(data)
#print(type(comdata1[0]))
comdata = re.compile(patcom).findall(data)
for j in range(0, len(comdata)):
print(eval('u"'+comdata[j]+'"'))
with open('西虹市首富.txt','a',encoding='utf8') as file:
file.write(comdata[j]+'\n')#这样才能转编码格式
#file.write(comdata[j].encode('utf-8').decode('unicode-escape')+'\n')#这样才能转编码格式
url="http://coral.qq.com/article/2671700896/comment/v2?callback=_article2671700896commentv2&orinum=10&oriorder=o&pageflag=1&cursor="+comdata1[0] |
|