2014-04-13 226 views
0

嗨,我正在运行一个程序来解析HTML地址中的表。它一切正常,我可以打印我提取的数据。但是,当我尝试写入数据的txt文件时,我收到下面的错误消息。任何人都可以帮助我吗?不知道我错过了什么。写入文本文件时出错Python

myfile.write(tds[0].text+ ","+ tds[4].text+ ","+ tds[7].text+ ","+ tds[12].text+ ","+ tds[14].text+ ","+ tds[17].text) 

错误:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "teste.py", line 14, in <module> 
    myfile.write(tds[0].text+ ","+ tds[4].text+ ","+ tds[7].text+ ","+ tds[12].text+ ","+ tds[14].text+ ","+ tds[17].text.encode('utf8')) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1: ordinal not in range(128) 

回答

0

你混合文本类型。您使用“,”和* .text方法来定义您希望写入文件的整个字符串。因此你在混合编码。而更优雅的解决方案存在于您的问题,快速和肮脏的方式来实现,这可能是使用:

str(tds[*]) 

tds[*].text() 
+0

当我使用STR(TDS [*])书写的作品,但它写入整个td标签不仅仅是信息。像这样: 31.21† – user3319895

+0

对不起,我错过了你的部分问题。现在听起来像你在谈论你如何解析html或xml。你以前用过美丽的汤:http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html – sahutchi

+0

是的,我使用的是美丽的。 – user3319895