lil*_*put 5 python-3.x python-requests anaconda
这是我的代码.
import requests
r = requests.get('https://academic.oup.com/journals')
Run Code Online (Sandbox Code Playgroud)
然后我得到了下面的错误;
ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",))
Run Code Online (Sandbox Code Playgroud)
为什么我收到错误?我该怎么办?该请求适用于其他网址,如https://www.google.com
尝试在HTTP标头中指定用户代理:
??? python3
Python 3.5.2 (default, Sep 14 2016, 11:28:32)
[GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> headers = requests.utils.default_headers()
>>> headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
>>> r = requests.get('https://academic.oup.com/journals', headers=headers)
>>> r
<Response [200]>
Run Code Online (Sandbox Code Playgroud)
另外,请确保遵循网站robots.txt中提到的规则
| 归档时间: |
|
| 查看次数: |
12225 次 |
| 最近记录: |