“UnicodeEncodeError:”尝试使用python将“£sign”写入excel表时出现ascii“codec”错误

2024-05-12 21:33:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中抓取一个£值,当我试图把它写到excel表中时,这个过程中断了,我得到了以下错误

UnicodeEncodeError:“ascii”编解码器无法对位置0中的字符u'\xa3'进行编码:序号不在范围内(128)

在cmd提示符中,$符号正在打印,没有任何错误。有人能建议我如何把价值(1750英镑)写进我的表格(有或没有英镑符号)。非常感谢。。。在

import requests
from bs4 import BeautifulSoup as soup
import csv


outputfilename = 'Ed_Streets2.csv'

outputfile = open(outputfilename, 'wb')
writer = csv.writer(outputfile)
writer.writerow([Rateable Value])

url = 'https://www.saa.gov.uk/search/?SEARCHED=1&ST=&SEARCH_TERM=city+of+edinburgh%2C+EDINBURGH&ASSESSOR_ID=&SEARCH_TABLE=valuation_roll_cpsplit&PAGE=0&DISPLAY_COUNT=100&TYPE_FLAG=CP&ORDER_BY=PROPERTY_ADDRESS&H_ORDER_BY=SET+DESC&ORIGINAL_SEARCH_TERM=city+of+edinburgh&DRILL_SEARCH_TERM=BOSWALL+PARKWAY%2C+EDINBURGH&DD_TOWN=EDINBURGH&DD_STREET=BOSWALL+PARKWAY#results'

response = session.get(url)                 
html = soup(response.text, 'lxml')
prop_link = html.find_all("a", class_="pagelink button small")

for link in prop_link:
     prop_url = base_url+(link["href"])

     response = session.get(prop_url)
     prop = soup(response.content,"lxml")

     RightBlockData = prop.find_all("div", class_="columns small-7 cell")
     Rateable_Value = RightBlockData[0].get_text().strip()

     print (Rateable_Value)

     writer.writerow([Rateable_Value])

Tags: csvimporturlsearchgetvalueresponse错误
1条回答
网友
1楼 · 发布于 2024-05-12 21:33:32

您需要将unicode对象明确地编码为字节。否则,您的系统将自动尝试使用ascii编解码器对其进行编码,这将以非ascii字符失败。所以,这个:

Rateable_Value = Rateable_Value.encode('utf8')

在你面前

^{pr2}$

应该会成功的。在

相关问题 更多 >