2017-02-28 100 views
2

我是一名python初学者。我写的代码如下:错误:UnicodeEncodeError:'gbk'编解码器无法编码字符集

from bs4 import BeautifulSoup 
import requests 

url = "http://www.google.com" 
response = requests.get(url) 
soup = BeautifulSoup(response.text, "html.parser") 
links = soup.find_all("a") 
for link in links: 
    print(link.text) 

当运行在Windows PowerShell中这个.py文件,打印(link.text)导致以下错误。

error: UnicodeEncodeError: 'gbk' codec can't encode charactor '\xbb' in position 5: 
illegal multibyte sequence. 

我知道错误是由一些中国文字引起的,它看起来像我应该使用“解码”或“忽略”,但我不知道如何解决我的代码。请帮助!谢谢!

回答

0

如果您不希望显示这些特殊字符:
您可以忽略它们:

print(link.text.encode(errors="ignore")) 
0

您可以编码字符串中utf8

for link in links: 
    print(link.text.encode('utf8')) 

但更好的方法是:

response = requests.get(url) 
soup = BeautifulSoup(response.text.encode("utf8"), "html.parser") 

要更多地了解你所面临的问题,你应该看看这个stackoverflow answer

相关问题