航仔咯 2020-12-31 23:40 采纳率: 100%
浏览 112
已采纳

跟b站某视频学习python爬虫学到代理开头就遇到一个问题想询问一下

以下是一段想用http://www.goubanjia.com/的免费ip试一下能否改变ip的代码

import requests
url='https://www.baidu.com/s?ie=utf-8&mod=1&isbd=1&isid=ade3ea7a000b60db&ie=utf-8&f=8&rsv_bp=1&tn=88093251_47_hao_pg&wd=ip&oq=ip&rsv_pq=ade3ea7a000b60db&rsv_t=5b017eIheFXHKmsg%2F8BPMMWIq2dAMuoGKKzn01j%2FnLb2GVMNMMAqZXe9NEUwYt0DqqdKmaMGZ7M1&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&bs=ip&rsv_sid=undefined&_ss=1&clist=505e05cef0e32bc5&hsug=&f4s=1&csor=2&_cr1=27777'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
# proxies对应的是http或s协议对应的ip
page_text=requests.get(url=url,headers=headers,proxies={"https":'120.83.109.201:9999'}).text
with open('./ip.html','w',encoding='utf-8')as aaa:
    aaa.write(page_text)

然后就遇到了这个错误

Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connection.py", line 170, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 96, in create_connection
    raise err
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 86, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 353, in connect
    conn = self._new_conn()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 182, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000021302D52400>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\python\lib\site-packages\urllib3\util\retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: /s?ie=utf-8&mod=1&isbd=1&isid=ade3ea7a000b60db&ie=utf-8&f=8&rsv_bp=1&tn=88093251_47_hao_pg&wd=ip&oq=ip&rsv_pq=ade3ea7a000b60db&rsv_t=5b017eIheFXHKmsg%2F8BPMMWIq2dAMuoGKKzn01j%2FnLb2GVMNMMAqZXe9NEUwYt0DqqdKmaMGZ7M1&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&bs=ip&rsv_sid=undefined&_ss=1&clist=505e05cef0e32bc5&hsug=&f4s=1&csor=2&_cr1=27777 (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000021302D52400>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/python/untitled/代理操作.py", line 5, in <module>
    page_text=requests.get(url=url,headers=headers,proxies={"https":'120.83.109.201:9999'}).text
  File "D:\python\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "D:\python\lib\site-packages\requests\adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: /s?ie=utf-8&mod=1&isbd=1&isid=ade3ea7a000b60db&ie=utf-8&f=8&rsv_bp=1&tn=88093251_47_hao_pg&wd=ip&oq=ip&rsv_pq=ade3ea7a000b60db&rsv_t=5b017eIheFXHKmsg%2F8BPMMWIq2dAMuoGKKzn01j%2FnLb2GVMNMMAqZXe9NEUwYt0DqqdKmaMGZ7M1&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t&bs=ip&rsv_sid=undefined&_ss=1&clist=505e05cef0e32bc5&hsug=&f4s=1&csor=2&_cr1=27777 (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000021302D52400>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))

想问一下怎么开启,我看了下代码和b站的某视频没差。

 

  • 写回答

1条回答 默认 最新

  • ayiya_Oese 2021-01-02 14:39
    关注
    ,proxies={"https":'120.83.109.201:9999'}

    这个删掉就好了,大概是代理失效了。或者自己去西刺找一个替换上去就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题