代码如下。我得到“urllib.error.HTTPError: HTTP 错误 404: 未找到”。
import urllib.request
import urllib.parse
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686)"
values = {'q': 'python programming tutorials'}
data = urllib.parse.urlencode(values)
url = 'https://www.google.com/search'+data
req = urllib.request.Request(url, headers = headers)
resp = urllib.request.urlopen(req)
resp_data = resp.read()
print(resp_data)
答案1
您的问题是您没有添加data
查询参数,可以通过在?
后面添加/search
这是您修改后并且可以正常工作的代码
import urllib.request
import urllib.parse
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686)"
values = {'q': 'python programming tutorials'}
data = urllib.parse.urlencode(values)
url = 'https://www.google.com/search?'+data
req = urllib.request.Request(url, headers = headers)
resp = urllib.request.urlopen(req)
resp_data = resp.read()
print(resp_data)
实际的区别在于你的url
>>> url
'https://www.google.com/searchq=python+programming+tutorials'
和我的修改url
>>> url
'https://www.google.com/search?q=python+programming+tutorials'
没有资源位于https://www.google.com/searchq这就是你得到的原因
urllib.error.HTTPError:HTTP 错误 404:未找到