我試圖從TripAdvisor吸取500家餐廳的列表;然而,第308餐廳後,我收到以下錯誤:UnicodeEncodeError:'ascii'編解碼器無法編碼字符u' u2019'在位置6:序號不在範圍內(128)
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)
我試過幾件事情我在計算器上找到,但沒有什麼工作截至目前。我想知道是否有人可以看看我的代碼,並看到任何可能的解決方案。
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'的行的內容需要是字符串或數字。另外,如[文檔](https://docs.python.org/2/library/csv.html)中所述,使用Python 2'csv'模塊時,應該以二進制模式打開CSV文件, 。您可能會發現這篇文章有用:[Pragmatic Unicode](http://nedbatchelder.com/text/unipain.html),它由SO老將Ned Batchelder編寫。 –