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 - OpenCV 在 Python 3 中通过套接字直播视频

python-3.x - Python find.line 不从文本文件中过滤日期字符串

javascript - 在 "Failed to execute ' : A history "上使用 fancybox 'History' PushState' 时发生历史记录推送错误

Python Telethon get_messages 偏移量

python - 将 .txt 文件从 .tar.gz 文件复制到其他目录

web-services - 我应该拒绝比预期更长的 URL 吗?

html - 检查 url 是否为 R 中的 HTML

python - pySerial 程序无法正确读取串行

python - 使用 xlsxwriter python 进行字符串验证

python - BeautifulSoup - 捕获特定类或文本的所有链接