python - urllib.error.HTTPError : HTTP Error 502: Bad Gateway PYTHON

标签 python python-3.x url python-3.6 urllib

我正在尝试使用 urllib.request.urlopen(url).read() 打开 Instagram URL,但出现错误 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')

我该如何解决这个问题?

编辑: 我得到了解决方案并已发布。干杯:)

最佳答案

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

关于python - urllib.error.HTTPError : HTTP Error 502: Bad Gateway PYTHON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58396922/

相关文章:

python - 几何矩阵乘法

python 3 : Trying to iterate lines of alphabet based on function of i

url - 如何从 txt 文件和使用 imacro 加载 url

python - 将整数从一个 def 编辑到另一个

python - 什么决定了以 .py 结尾的 URL 将显示为脚本本身还是脚本生成的页面?

ios - 如何使用 swift 下载和共享 pdf 文件

python - 如何返回与字典中最小值对应的键列表

python - 对 numpy 数组的行求和,其中每个和的起始索引来自另一个数组

python - 删除特定的键但不删除值(列表),因此它成为上键的元素

python - plotly/破折号 : is it possible to hide tick labels of a secondary y axis?