我正在尝试从TripAdvisor获取阿姆斯特丹的500家餐厅名单,但在第308家餐厅后,我收到了以下错误提示:
Traceback (most recent call last):
File "C:/Users/dtrinh/PycharmProjects/TripAdvisorData/LinkPull-HK.py", line 43, in <module>
writer.writerow(rest_array)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 6: ordinal not in range(128)
我尝试了在StackOverflow上找到的几种方法,但目前没有任何有效的解决方案。我想知道是否有人可以查看我的代码,并提供潜在的解决方案。
for item in soup2.findAll('div', attrs={'class', 'title'}):
if 'Cuisine' in item.text:
item.text.strip()
content = item.findNext('div', attrs=('class', 'content'))
cuisine_type = content.text.encode('utf8', 'ignore').strip().split(r'\xa0')
rest_array = [account_name, rest_address, postcode, phonenumber, cuisine_type]
#print rest_array
with open('ListingsPull-Amsterdam.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow(rest_array)
break
cuisine_type
是一个列表,因为你使用了.split
(而且我不确定为什么要在非断点空格上分割...)。然而,传递给.writerow
的行的内容需要是字符串或数字。此外,在使用 Python 2 的csv
模块时,应该以二进制模式打开 CSV 文件,如文档中所述。你可能会发现这篇文章有帮助:实用 Unicode,它是由 SO 老手 Ned Batchelder 写的。 - PM 2Ring