我在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())
答案 0 :(得分:0)
在您的代码中,使用encode
方法
代码:
yelp_soup = BeautifulSoup(yelp_r.text.encode('utf-8'), 'html.parser')