参考: https://blog.csdn.net/itbiggod/article/details/102866720
# 运行异常代码
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import random
base_url = "https://baike.baidu.com"
his = ['/item/网络爬虫/5162711?fr=aladdin']
# 打印网页内容 + 网页名字
url = base_url + his[-1]
html = urlopen(url).read().decode('utf-8')
soup = BeautifulSoup(html, features='lxml')
print(soup.find('h1').get_text(), ' url: ', his[-1])
运行报: UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-13: ordinal not in range(128)
问题原因:
python3 urlopen()链接地址中不能出现中文,而上述代码的 word 是传入的中文字符,终于找到了问题的所在。
# 更改后代码 from bs4 import BeautifulSoup from urllib.request import urlopen import re import random from urllib.parse import quote import string base_url = "https://baike.baidu.com" his = ['/item/网络爬虫/5162711?fr=aladdin'] # 打印网页内容 + 网页名字 url = quote(base_url + his[-1], safe=string.printable) html = urlopen(url).read().decode('utf-8') soup = BeautifulSoup(html, features='lxml') print(soup.find('h1').get_text(), ' url: ', his[-1])
到此解决
额外知识
python3 reload(sys)会出现错误提示
python2.x中:
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8’)
# 可解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range
python3.x中:
import importlib,sys
importlib.reload(sys)