LUYAO123 发表于 2019-7-21 22:16:23

爬虫时遇到问题:urlopen error [WinError 10060]

我在爬取腾讯视频评论时遇到这种情况,求大佬指点,以下是报错的码,最下面是我写的代码
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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

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 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>



我的代码:
# -*- 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))
    comdata = re.compile(patcom).findall(data)
    for j in range(0, len(comdata)):
      print(eval('u"'+comdata+'"'))
      with open('西虹市首富.txt','a',encoding='utf8') as file:
            file.write(comdata+'\n')#这样才能转编码格式
            #file.write(comdata.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

新手·ing 发表于 2019-7-22 07:10:56

由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

fallout.nj 发表于 2019-7-22 11:11:39

qq应该有反扒吧,你这光秃秃的直接裸连,怕是被检测到了吧,加个headers头呀

漫步云端〃 发表于 2019-7-22 16:56:01

就是在网上复制的代码吧。

wcr0021 发表于 2020-8-4 00:28:20

帮顶一下 我也遇到相同的问题了
urllib.error.URLError: <urlopen error 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>
就是这个

陈尚涵 发表于 2020-8-5 18:40:21

wcr0021 发表于 2020-8-4 00:28
帮顶一下 我也遇到相同的问题了
urllib.error.URLError:
就是这个

0点熬夜?!{:10_277:}注意身体啊!

wsl886 发表于 2020-9-28 14:33:55

1

bonst 发表于 2020-9-28 17:54:37

你带开chrome的审查网页,抓包看看headers需要哪些东西,一般有host,user-agent,referer,cookie还有更多参数可以添加进去,防止爬取失败
页: [1]
查看完整版本: 爬虫时遇到问题:urlopen error [WinError 10060]