鱼C论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 129|回复: 3

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

[复制链接]
最佳答案
0 
发表于 2019-7-21 22:16:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有帐号?立即注册

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]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
477 
发表于 2019-7-22 07:10:56 | 显示全部楼层
由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
3 
发表于 2019-7-22 11:11:39 | 显示全部楼层
qq应该有反扒吧,你这光秃秃的直接裸连,怕是被检测到了吧,加个headers头呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
2 
发表于 2019-7-22 16:56:01 | 显示全部楼层
就是在网上复制的代码吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

小甲鱼强烈推荐上一条 /1 下一条

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号

GMT+8, 2019-8-21 10:30

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表