我试图在谷歌搜索通过参数,它的工作时,我搜索一个字,但一个我做空间它打破了我知道有一种方法来编码的网址。你知道吗
import urllib.request
from urllib.parse import urlencode, quote_plus
from fake_useragent import UserAgent
import time
import requests
from bs4 import BeautifulSoup
keyword = "host free"
url = "https://www.google.co.il/search?q=%s" % (keyword)
print(url)
thepage = urllib.request.Request(url, headers=request_headers)
page = urllib.request.urlopen(thepage)
//Continue...
回溯:
https://www.google.co.il/search?q=host free
Traceback (most recent call last):
File "C:\Users\Maor Ben Lulu\Desktop\Maor\Python\google\Google_Bot_new.py", line 42, in <module>
page = urllib.request.urlopen(thepage)
File "C:\Program Files (x86)\Python37-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Program Files (x86)\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Program Files (x86)\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Program Files (x86)\Python37-32\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Program Files (x86)\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Program Files (x86)\Python37-32\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
[Finished in 0.7s with exit code 1]
[shell_cmd: python -u "C:\Users\Maor Ben Lulu\Desktop\Maor\Python\google\Google_Bot_new.py"]
[dir: C:\Users\Maor Ben Lulu\Desktop\Maor\Python\google]
[path: C:\Program Files (x86)\Python37-32\Scripts\;C:\Program Files (x86)\Python37-32\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Program Files\Git\cmd;C:\Users\Maor Ben Lulu\AppData\Local\Microsoft\WindowsApps;]
我也曾用希伯来语写过这样一句话:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-18: ordinal not in range(128)
有一种方法可以用urllib.parse.quote对url进行编码 但是有一个requests模块在所有这些情况下都非常有用,您可以按如下方式使用它:
如上所示,可以将查询参数作为关键字参数传递
相关问题 更多 >
编程相关推荐