我想刮一个网站与德语变音在网址。这是我用python3.3编写的代码,它没有任何元音变化都能很好地工作。在
def numResults(keyword):
try:
page_google = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' +keyword
print(page_google)
req_google = Request(page_google)
req_google.add_header('User Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120427 Firefox/15.0a1')
html_google = urlopen(req_google).read()
soup = BeautifulSoup(html_google)
except URLError as e:
print(e)
return soup
但当我要求这样的东西时:
^{pr2}$我得到以下错误,因为urllib无法处理我猜的元音变音:
Traceback (most recent call last):
File "C:\Users\zwieback86\Desktop\programming\scrape.py", line 137, in <module>
print(numResults('älterer'))
File "C:\Users\zwieback86\Desktop\programming\scrape.py", line 73, in numResults
html_google = urlopen(req_google).read()
File "c:\python33\lib\urllib\request.py", line 156, in urlopen
return opener.open(url, data, timeout)
File "c:\python33\lib\urllib\request.py", line 469, in open
response = self._open(req, data)
File "c:\python33\lib\urllib\request.py", line 487, in _open
'_open', req)
File "c:\python33\lib\urllib\request.py", line 447, in _call_chain
result = func(*args)
File "c:\python33\lib\urllib\request.py", line 1268, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "c:\python33\lib\urllib\request.py", line 1248, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "c:\python33\lib\http\client.py", line 1061, in request
self._send_request(method, url, body, headers)
File "c:\python33\lib\http\client.py", line 1089, in _send_request
self.putrequest(method, url, **skips)
File "c:\python33\lib\http\client.py", line 953, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 38: ordinal not in range(128)
当我输入地址时 “http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=过滤器” 在浏览器里我得到了想要的页面。在
所以我假设urllib不能处理url中带有元音变音的请求。但是我怎么才能让它接受德语变音调呢?改变诸如a->;ae这样的元音变化不是一个选择。在
非常感谢和问候!在
使用请求模块!它优于urllib2。http://docs.python-requests.org/en/latest/
相关问题 更多 >
编程相关推荐