我正在尝试以这种方式发出网络请求:
import urllib.request, urllib.parse, urllib.error
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
long = 'https://www.elespectador.com/deportes/futbol-colombiano'
if len(long) < 60:
data_2 = urllib.request.urlopen(long, context = ctx).read().decode()
但是我得到了这个错误:
File "C:\Users\acer\Anaconda3\lib\urllib\request.py", line 650, in
http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
HTTPError: Bad Request
我不知道如何处理这个问题或出了什么问题,除了有时会运行,但只有很少的时间。 我需要解决它。 谢谢!
最佳答案
这将为您处理 SSL 内容, 但我认为它只适用于 Pip 9 及以下版本:
from pip._vendor import requests
link = "https://www.elespectador.com/deportes/futbol-colombiano"
answer = requests.get(link)
answer_text = answer.text
关于python - HTTPError : Bad Request with urllib. request.urlopen python 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431493/