编解码器无法使用Beautiful soup 4 python 3编码字符错误

时间:2017-12-15 04:43:49

标签: python web-scraping beautifulsoup

我在Beatiful Soup 4和Python 3中一直收到以下错误。

追踪(最近一次呼叫最后一次):

File "<stdin>", line 1, in <module>
File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
 return codecs.charmap_encode(input,self.errors,encoding_map)[0]
 UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 33362: character maps to <undefined>

实际代码:

import requests
from bs4 import BeautifulSoup
url='https://www.yelp.com/search?find_desc=Restaurants&find_loc='
yelp_r = requests.get(url)
yelp_soup = BeautifulSoup(yelp_r.text, 'html.parser')
print(yelp_soup.prettify())

1 个答案:

答案 0 :(得分:0)

在您的代码中,使用encode方法

添加文本

代码:

yelp_soup = BeautifulSoup(yelp_r.text.encode('utf-8'), 'html.parser')