python - python请求得到错误61连接被拒绝但邮递员使用相同的GET请求
问题描述
在我发布这个之前,我花了 2 小时搜索了一个解决方案,但没有一个成功,所以最后我在这里
我尝试使用requests
并urllib.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'))
解决方案
从 https 中删除 s 对我有用
推荐阅读
- amazon-web-services - 什么策略可以让 Lightsail 上传到受限的 S3 存储桶?
- tensorflow - tensorflow 仅在 anaconda 的虚拟环境中可用
- sql - 如何在分层查询中进行连接
- nginx - 为 Vue.js 分发项目配置 nginx
- mysql - MySQL 空间查询,其中行/位置具有不同的半径
- r - 我的矩阵填充代码给出了 NA?
- c# - 将 Type 定义为 Class.Properties 中的任何一个
- c++ - 删除多组中的重复项
- c# - Visual Studio 2017 编译器错误 System.WebExtension 未定义
- python - 数据具有相同的字符,我将其拆分