python - 如何修复 HTTP 错误 400 : Bad Request?

标签 python python-3.x bad-request

我正在尝试网络抓取,这是我的代码。

出于某种原因,我收到 HTTP 错误 400:错误请求,我以前从未遇到过这种情况。

有什么想法吗?

这是我的代码:

import urllib.request
import re

url = ('https://www.myvue.com/whats-on')

req = urllib.request.Request(url, headers={'User Agent': 'Mozilla/5.0'})

def main():

    html_page = urllib.request.urlopen(req).read()

    content=html_page.decode(errors='ignore', encoding='utf-8')

    headings = re.findall('<th scope="col" abbr="(.*?)">', content)

    print(headings)

main()

最佳答案

修复标题:

req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})

User-Agent,不是User Agent


此外,我建议切换到 requests模块。

import requests
html_page = requests.get(url, {'User-Agent': 'Mozilla/5.0'}).text

这相当于三行 urllib 并且更具可读性。此外,它会自动为您解码内容。

关于python - 如何修复 HTTP 错误 400 : Bad Request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45058583/

相关文章:

Python:转换小时分钟秒的行程持续时间并仅保留分钟数

python-3.x - 为什么无法使用纯Python下载字幕?

python - 如何在 Vim 缓冲区中打印 python 代码的输出?

python - 在 python 中读取 v 7.3 mat 文件

python - 在 Python 中检查属于斐波那契数列的输入

php - Guzzle:处理 400 个错误请求

c# - 400 错误请求,Azure 服务管理 api 配置更改

sharepoint - 尝试使用 Microsoft Graph api 搜索 Sharepoint 文件

python - 如何在 JupyterLab 中保存笔记本的当前状态

Python 3.2 在 csv.DictReader 中跳过一行