HTTPError: HTTP Error 404: Not Found in python 3

HTTPError: HTTP Error 404: Not Found in python 3

import urllib.request

def get_site_html(url):
    source = urllib.request.urlopen(url).read()
    return source

我正在尝试创建此函数,但它抛出以下内容 error-- HTTPError: HTTP Error 404: Not Found

请帮我解决这个问题。

import urllib.request

req = urllib.request.Request('your link here')
with urllib.request.urlopen(req) as response:
   the_page = response.read()

来源:https://docs.python.org/3/howto/urllib2.html

import urllib.request
requ = urllib.request.Request(url='')
with urllib.request.urlopen(requ) as f:
data = f.read()

试试这个,否则你可能需要一个 .decode 语句和 .read