我在python中使用BS4解析html表。一切都很好,我可以确定所有我需要的元素并打印出来。但是程序停止工作,然后我尝试将结果写入文本文件。我得到这个错误:"UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 13: ordinal not in range(128)"
我曾尝试在写命令中使用.encode('utf-8'),但我得到的结果是这样写的:31.61†
这就是我要做的。我使用代码结构来解析另一个表,它成功了。如果有人能给我指出正确的方向,我会很感激。在from threading import Thread
import urllib2
import re
from bs4 import BeautifulSoup
url = "http://trackinfo.com/dog-racelines.jsp?page=1&runnername=Ww%20Gloriaestefan"
myfile = open('base/basei/' + url[57:].replace("%20", " ").replace("%27","'") + '.txt','w+')
soup = BeautifulSoup(urllib2.urlopen(url).read())
for tr in soup.find_all('tr')[0:]:
tds = tr.find_all('td')
if len(tds) >=0:
print tds[0].text, ",", tds[4].text, ",", tds[7].text, ",", tds[12].text, ",", tds[14].text, ",", tds[17].text
myfile.write(tds[0].text + ','+ tds[4].text + "," + tds[7].text + "," + tds[12].text + "," + tds[14].text + "," + tds[17].text)
myfile.close()