2017-08-04 57 views
-4

這是我的代碼,我抓取99acres網站的數據並將其存儲在csv文件中,但是當我這樣做它給了我「字符映射」編解碼器不能編碼字符「\ u20b9」 ......請告訴我怎麼會得到解決......的錯誤信息UnicodeEncodeError:'charmap'編碼解碼器無法在位置5編碼字符' u20b9':字符映射到<undefined>

import io 
import csv 
import requests 
from bs4 import BeautifulSoup 

response = requests.get('https://www.99acres.com/search/property/buy/residential-all/hyderabad?search_type=QS&search_location=HP&lstAcn=HP_R&lstAcnId=0&src=CLUSTER&preference=S&selected_tab=1&city=269&res_com=R&property_type=R&isvoicesearch=N&keyword_suggest=hyderabad%3B&fullSelectedSuggestions=hyderabad&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsiaHlkZXJhYmFkIiwiQ0lUWV8yNjksIFBSRUZFUkVOQ0VfUywgUkVTQ09NX1IiXX1d&texttypedtillsuggestion=hyder&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_269%2C%20PREFERENCE_S%2C%20RESCOM_R&searchform=1&price_min=null&price_max=null') 

html = response.text 

soup = BeautifulSoup(html, 'html.parser') 

list=[] 

dealer = soup.findAll('div',{'class': 'srpWrap'}) 


for item in dealer: 
    try: 
     p = item.contents[1].find_all("div",{"class":"_srpttl srpttl fwn wdthFix480 lf"})[0].text 
    except: 
     p='' 
    try: 
      d = item.contents[1].find_all("div",{"class":"lf f13 hm10 mb5"})[0].text 
    except: 
      d='' 
    li=[p,d] 
    list.append(li) 

with io.open('project.csv','w',encoding="utf-8") as file: 
    writer= csv.writer(file) 
    for row in list: 
     writer.writerows(row) 
file.close() 
+0

是的,那是你的代碼。你想告訴我們什麼? – asongtoruin

+0

當我運行這段代碼時,它給了我一個錯誤,我在下面發帖......而且我無法解除它,所以我需要你的幫助才能擺脫這個。 「」 回溯(最近最後一次通話): 文件 「C:\用戶\ Divyam \桌面\ Python的\ project.py」,第27行,在 writer.writerow(行) 文件「C:\用戶\ Divyam \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ encodings \ cp1252.py「,第19行,編碼爲 返回codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError:' charmap'編解碼器無法在位置5編碼字符'\ u20b9':字符映射到「」 –

+0

請修改您的問題以包含錯誤信息,請勿將它發佈在無法正確格式化的註釋中很難找到。 –

回答

0

用途:.encode(encoding = 'UTF-8')

+0

y它刪除錯誤,但我仍然沒有得到該文件 –

+3

歡迎來到[so]!請說明您的解決方案的工作原理這看起來很明顯,但事實並非如此:編碼是一個敏感的話題,很多人都不太明白。 – jpaugh

相關問題