2017-02-28 101 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

相關問題