l3142600073 发表于 2019-8-4 22:25:53

UnicodeEncodeError:'ascii' codec can't encode...ordinal not in range(128)

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()
      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 = ''.join()
      print(content)

if __name__ == "__main__":
    main()

cct 发表于 2019-8-23 11:17:03

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()
      tmp = each["href"].split('=')
      if len(tmp) == 2:
            each["href"] = ''.join(, urllib.parse.urlencode({'':tmp})])
      url2 = ''.join(["http://baike.baidu.com", each["href"]])
      response2 = urllib.request.urlopen(url=url2)
      html2 = response2.read().decode("utf-8")
      soup2 = BeautifulSoup(html2, "html.parser")
      
      if soup2.h2:
            content = ''.join()
      content = ''.join()
      print(content)

if __name__ == "__main__":
    main()

each["href"]这个里面有可能有中文,你需要转码

bravsheng 发表于 2021-8-21 17:38:25

本帖最后由 bravsheng 于 2021-8-21 17:41 编辑

cct 发表于 2019-8-23 11:17
each["href"]这个里面有可能有中文,你需要转码

正解!我也遇到这个问题了,原因就是中文需要转码。 转码后就可以了!
页: [1]
查看完整版本: UnicodeEncodeError:'ascii' codec can't encode...ordinal not in range(128)