首页 > 解决方案 > python请求得到错误61连接被拒绝但邮递员使用相同的GET请求

问题描述

在我发布这个之前,我花了 2 小时搜索了一个解决方案,但没有一个成功,所以最后我在这里

我尝试使用requestsurllib.request放入user-agent标题但仍然得到ConnectionRefusedError: [Errno 61] Connection refused
但是它在邮递员中工作

我试图获取 html 页面内容的 url 是https://fangj.github.io/friends/season/0101.html

为什么这段代码不起作用?

import sys
import json
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

ua = UserAgent()

headers = {
    'user-agent' : ua.chrome
}

url = 'https://fangj.github.io/friends/season/0101.html'

res = requests.get(url, headers=headers)

print(res.content)

错误如下

/usr/bin/python3 "/Users/Dave/Desktop/SAT Preprocess/crawler.py"
Traceback (most recent call last):
  File "/Library/Python/3.7/site-packages/urllib3/connection.py", line 170, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/Library/Python/3.7/site-packages/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/Library/Python/3.7/site-packages/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Python/3.7/site-packages/urllib3/connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "/Library/Python/3.7/site-packages/urllib3/connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "/Library/Python/3.7/site-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "/Library/Python/3.7/site-packages/urllib3/connection.py", line 353, in connect
    conn = self._new_conn()
  File "/Library/Python/3.7/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 0x103cb8b38>: Failed to establish a new connection: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Dave/Library/Python/3.7/lib/python/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/Library/Python/3.7/site-packages/urllib3/connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/Library/Python/3.7/site-packages/urllib3/util/retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='fangj.github.io', port=443): Max retries exceeded with url: /friends/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x103cb8b38>: Failed to establish a new connection: [Errno 61] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Dave/Desktop/SAT Preprocess/crawler.py", line 20, in <module>
    res = requests.get(url, headers=headers)
  File "/Users/Dave/Library/Python/3.7/lib/python/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/Dave/Library/Python/3.7/lib/python/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/Dave/Library/Python/3.7/lib/python/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/Dave/Library/Python/3.7/lib/python/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/Users/Dave/Library/Python/3.7/lib/python/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='fangj.github.io', port=443): Max retries exceeded with url: /friends/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x103cb8b38>: Failed to establish a new connection: [Errno 61] Connection refused'))

标签: pythonpython-requestspostman

解决方案


从 https 中删除 s 对我有用


推荐阅读