def花 发表于 2020-8-21 07:01:55

编码问题

import urllib.request
from bs4 import BeautifulSoup
import re


def main():
    url = 'http://baike.baidu.com/item/百度/6699'
    response = urllib.request.urlopen(url)
    html = response.read()
    html.encoding = 'utf-8'

    soup = BeautifulSoup(html,"html.parser")
    result = soup.select('meta.description')
    for each in result:
      print(each)

if __name__ == '__main__':
    main()

报错:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-11: ordinal not in range(128)

zltzlt 发表于 2020-8-21 07:03:16

https://fishc.com.cn/forum.php?mod=viewthread&tid=177175
页: [1]
查看完整版本: 编码问题