|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Traceback (most recent call last):
File ".\41_2_Beautiful_Soup.py", line 27, in <module>
main()
File ".\41_2_Beautiful_Soup.py", line 17, in main
response2 = urllib.request.urlopen(url2)
File "C:\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Python37\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Python37\lib\urllib\request.py", line 543, in _open
'_open', req)
File "C:\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Python37\lib\urllib\request.py", line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python37\lib\urllib\request.py", line 1317, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "C:\Python37\lib\http\client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Python37\lib\http\client.py", line 1240, in _send_request
self.putrequest(method, url, **skips)
File "C:\Python37\lib\http\client.py", line 1107, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 36-39: ordinal not in range(128)
import urllib.request
import urllib.parse
import re
from bs4 import BeautifulSoup
def main():
keward = input("请输入关键字:")
keward = urllib.parse.urlencode({"word":keward})
response = urllib.request.urlopen("http://baike.baidu.com/search/word?%s" %keward)
html = response.read()
soup = BeautifulSoup(html, "html.parser")
for each in soup.find_all(href = re.compile("view")):
content = ''.join([each.text])
url2 = ''.join(["http://baike.baidu.com", each["href"]])
response2 = urllib.request.urlopen(url2)
html2 = response2.read().decode('utf-8')
soup2 = BeautifulSoup(html2, "html.parser")
if soup2.h2:
content = ''.join([content, soup2.h2.text])
content = ''.join([content, "->", url2])
print(content)
if __name__ == "__main__":
main()
|
|