HTTPError问题:urllib.request.urlopen python请求错误

胡安·戴维(Juan David)

我正在尝试以这种方式发出网络请求:

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

我不知道如何处理这个问题或出了什么问题,除了有时会运行,而且只有很少的时间。我要解决 谢谢!

胡安·戴维(Juan David)

我用一个循环解决了这个问题,它使请求太多次,直到没有问题为止:

long = 'http://www.elcolombiano.com/deportes/futbol-europeo'

while data_0 is None:
contador = contador + 1
try:
    data_0 = urllib.request.urlopen(long, context = ctx).read().decode()
    soup2 = BeautifulSoup(data_0, "html.parser")
    tags_2 = soup2('a')
except:
    print(contador)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python 3.6.2 url.request.urlopen() urllib.error.HTTPError:HTTP 错误 403:禁止

将 urllib.request.urlopen 移动到请求

python3 安全问题,os.system('wget...') 与 urllib.request.urlopen(...)

HTTP错误401:未经授权使用urllib.request.urlopen

python:urllib.request.urlopen不起作用

Python 3:urllib.request.urlopen和Progressbar

urllib.request 的 urlopen 无法在 python 3.7 中打开页面

Python请求lib工作,urllib.request.urlopen POST不工作

urllib.error.HTTPError: HTTP Error 404: Not Found when using request.urlopen()

python: urllib.request.urlopen() HTTP 错误 308 永久重定向

Python 3-urllib.request-HTTPError

我需要使用urllib2.Request / urlopen处理哪些错误/异常?

Python urllib.request.urlopen:AttributeError:“ bytes”对象没有属性“ data”

python3中来自urllib.request的urlopen的奇怪行为

urllib.request.urlopen(url) 在 python 3 中不起作用

urllib HTTPS请求:<urlopen错误未知的URL类型:https>

python:urllib2如何使用urlopen请求发送cookie

urllib urlopen / urlretrieve打开文件过多错误

带有验证的urllib.request.urlopen(url)

如何处理来自urllib.request.urlopen()的响应编码

模拟urllib.request.urlopen的read函数返回MagicMock签名

替换urllib.request.urlopen(url,ca *)的开启器

urllib.request.urlopen 不适用于特定网站

Python urllib urlopen不起作用

Python文档:urllib.request.urlopen(URL,data = None,[timeout,] *,cafile = None中的方括号是什么意思?

urllib2.HTTPError:HTTP错误400:错误的请求-Python

urllib,urllib2,urllib.request.open,AttributeError:“模块”对象没有属性“ urlopen”

Python 网页抓取错误 403 + urlopen 解码问题

python urlopen 返回错误