urllib.error.HTTPError:HTTP 错误 502:错误的网关 PYTHON [英] urllib.error.HTTPError: HTTP Error 502: Bad Gateway PYTHON

查看:113
本文介绍了urllib.error.HTTPError:HTTP 错误 502:错误的网关 PYTHON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 urllib.request.urlopen(url).read() 打开 Instagram URL,但出现错误 urllib.error.HTTPError: HTTP Error 502: Bad Gateway

I am trying to open the Instagram URL using urllib.request.urlopen(url).read() but I am getting the error urllib.error.HTTPError: HTTP Error 502: Bad Gateway

username = input('enter the username - ')
url = "https://www.instagram.com/{}".format(username)
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')

我该如何解决这个问题?

How can I solve this issue?

我得到了解决方案,并已发布.干杯:)

I got the solution and I have posted it. Cheers :)

推荐答案

首先添加
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
然后输入
html = urllib.request.urlopen(req).read()
这样就可以解决问题了.以前它无法验证请求的来源.我们欺骗它认为该请求是从真正的浏览器(即 Mozilla)发出的.

First, add
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
then type
html = urllib.request.urlopen(req).read()
This will solve the problem. Previously it couldn't verify the origin of the request. We tricked it into thinking that the request is made from a genuine browser, i.e. Mozilla.

这篇关于urllib.error.HTTPError:HTTP 错误 502:错误的网关 PYTHON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆