鱼C论坛

 找回密码
 立即注册
查看: 1178|回复: 2

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

[复制链接]
发表于 2019-8-4 22:25:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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([each.text])
        tmp = each["href"].split('=')
        if len(tmp) == 2:
            each["href"] = ''.join([tmp[0], urllib.parse.urlencode({'':tmp[1]})])
        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, soup2.h2.text])
        content = ''.join([content, "->", url2])
        print(content)

if __name__ == "__main__":
    main()

each["href"]这个里面有可能有中文,你需要转码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-8-21 17:38:25 | 显示全部楼层
本帖最后由 bravsheng 于 2021-8-21 17:41 编辑
cct 发表于 2019-8-23 11:17
each["href"]这个里面有可能有中文,你需要转码


正解!  我也遇到这个问题了,原因就是中文需要转码。 转码后就可以了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-13 13:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表