How do I fix a HTTP Error 400: Bad Request?

北城余情 提交于 2019-12-23 02:32:37

问题


I am trying to web scrape, here is my code.

For some reason I am getting HTTP Error 400: Bad Request, I have never had this before.

Any ideas?

Here is my code:

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()

回答1:


Fix your header:

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

It's User-Agent, not User Agent.


Additionally, I would recommend switching over to the requests module.

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

This is the equivalent of three lines of urllib and much more readable. In addition, it automatically decodes the content for you.



来源:https://stackoverflow.com/questions/45058583/how-do-i-fix-a-http-error-400-bad-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!