我想從PGA網站上刮取數據以獲得美國所有高爾夫球場的列表。我想抓取數據並輸入到CSV文件中。我的問題是運行我的腳本後,我得到這個錯誤。任何人都可以幫助解決這個錯誤,以及我如何能夠提取數據?UnicodeEncodeError:使用Python和beautifulsoup4刮取數據
以下是錯誤消息:
File "/Users/AGB/Final_PGA2.py", line 44, in
writer.writerow(row)UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 35: ordinal not in range(128)
腳本下面;
import csv
import requests
from bs4 import BeautifulSoup
courses_list = []
for i in range(906): # Number of pages plus one
url = "http://www.pga.com/golf-courses/search?page={}&searchbox=Course+Name&searchbox_zip=ZIP&distance=50&price_range=0&course_type=both&has_events=0".format(i)
r = requests.get(url)
soup = BeautifulSoup(r.content)
g_data2=soup.find_all("div",{"class":"views-field-nothing"})
for item in g_data2:
try:
name = item.contents[1].find_all("div",{"class":"views-field-title"})[0].text
print name
except:
name=''
try:
address1=item.contents[1].find_all("div",{"class":"views-field-address"})[0].text
except:
address1=''
try:
address2=item.contents[1].find_all("div",{"class":"views-field-city-state-zip"})[0].text
except:
address2=''
try:
website=item.contents[1].find_all("div",{"class":"views-field-website"})[0].text
except:
website=''
try:
Phonenumber=item.contents[1].find_all("div",{"class":"views-field-work-phone"})[0].text
except:
Phonenumber=''
course=[name,address1,address2,website,Phonenumber]
courses_list.append(course)
with open ('PGA_Final.csv','a') as file:
writer=csv.writer(file)
for row in courses_list:
writer.writerow(row)
您能編輯您的文章以正確顯示嗎?如果您將整個東西縮進4個空格,它將顯示爲代碼塊而不是未格式化的文本。 –
我編輯了帖子,等待批准。 – Leb
http://stackoverflow.com/questions/30551429/error-writing-data-to-csv-due-to-ascii-error-in-python/30551550#30551550 –