【Wikipedia爬虫工具包的使用】请求超时、代理错误、SSLError

项目场景:

https://github.com/goldsmith/Wikipedia

使用该工具包爬取维基百科的搜索内容


问题描述

TimeoutError;urllib3.exceptions.NewConnectionError;requests.exceptions.ConnectionError;requests.exceptions.ProxyError

case1:Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

case2:Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

case3:Caused by ProxyError(‘Cannot connect to proxy.’, RemoteDisconnected(‘Remote end closed connection without response’))。


尝试:

1.使用v2ray开启代理,尝试多个节点均请求失败(使用proxies = { #ip: port },开启或关闭v2ray代理服务均失败。浏览器搜IP地址显示的非真实地址)。
2.尝试了利用my_fake_useragent更换随机UA,没有变化。
3.设置verify=False也没有变化。
4.设置’Connection’: 'close’也没有变化。
5.更换url为http://baidu.com,可以请求但因为网页内容问题无法返回json格式数据。

// proxies, verify=False
proxies = {
    'http': 'xxx.xx.xxx.xxx:xxxxx', 'https': 'xxx.xx.xxx.xxx:xxxxx',
  }
  
  r = requests.get(API_URL, params=params, headers=headers, proxies=proxies, verify=False)
  
//my_fake_useragent
  import my_fake_useragent as ua
  USER_AGENT = ua.UserAgent().random()
  
//'Connection': 'close'
headers = {
    'User-Agent': USER_AGENT,
    'Connection': 'close'
  }

解决方案:

更换clash开启代理(不用proxies, 只使用clash开启代理服务即可。浏览器搜ip地址显示的是真实地址)

出现 Caused by SSLError(SSLError(1, '[SSL: KRB5_S_TKT_NYV] unexpected eof while…错误。
代码中注释提示 # This branch is for urllib3 v1.22 and later.
将urllib3换成1.21版本,成功运行。

verify=False时会有警告InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. 删掉verify=False,警告消失。
取消之前尝试的修改(‘Connection’: 'close’和my_fake_useragent更换随机UA),也可以成功运行。

//均为工具包中原始代码
API_URL = 'http://en.wikipedia.org/w/api.php'
USER_AGENT = 'wikipedia (https://github.com/goldsmith/Wikipedia/)'

          ......

headers = {
    'User-Agent': USER_AGENT
  }
r = requests.get(API_URL, params=params, headers=headers)

总结

使用该工具包,无需对wikipedia.py进行修改。开启合适的代理即可。

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值