【问题标题】:Parse html table and write a text file in python解析html表格并在python中编写一个文本文件
【发布时间】:2017-02-18 17:56:32
【问题描述】:

我在 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() 

【问题讨论】:

  • 我在 Windows 7 和 Ubuntu 上都尝试过您的代码,但无法显示格式错误的字符;我只是在 31 之后得到一个空格。也许你应该添加更多关于你如何运行代码的细节(操作系统、你如何运行你的脚本以及你如何查看文本文件)。否则,您可以尝试stackoverflow.com/questions/10993612/… 中的建议之一,只是破坏数据以提交。
  • 我在mac os雪豹终端中使用python
  • 问题是我解析的标签(31.14 )有这个&nbsp。在文本之后留下一个空格,它会扰乱写作并产生问题。我不知道如何绕过或删除标签中的这个 &nbsp

标签: python web-scraping beautifulsoup html-table fwrite


【解决方案1】:

下面的代码对我有用。我用逗号替换了不间断空格;通过这种方式,您可以直接将输出用作 CSV(例如,您可以轻松读取 Excel 或 LibreOffice Calc)。

import urllib2                                                                  
from bs4 import BeautifulSoup                                                   

url = "http://trackinfo.com/dog-racelines.jsp?page=1&runnername=Ww%20Gloriaestefan"
soup = BeautifulSoup(urllib2.urlopen(url).read())                               

with open('out.txt', 'w') as myfile:                                           
    for tr in soup.find_all('tr')[0:]:
        tds = tr.find_all('td')
        if len(tds) >= 0:
            stripped_tds = [tds[x].text.strip() for x in (0, 4, 7, 12, 14, 17)]
            out = ','.join(stripped_tds)
            out = out.replace(u'\xa0', ',')
            print out
            myfile.write(out + '\n')

(with 语句消除了显式调用myfile.close() 的需要。当with 内的代码部分完成时,它会隐式执行此操作,即使它在那里遇到异常也是如此。)

out.txt的内容:

2014-04-15,E5,31.28,7,6,32.18,C
2014-04-13,E6,31.07,2,4,31.64,B
2014-04-11,E6,31.21,6,6,32.53,B
2014-04-07,E7,30.93,5,7,32.31,B
2014-04-03,S1,30.82,3,2,31.23,
2014-03-30,E9,31.02,3,8,31.97,A
2014-03-28,E9,30.95,7,8,31.85,A
2014-03-23,E9,30.88,8,8,32.06,A
2014-03-21,E6,30.83,1,1,30.83,SB
2014-03-17,E5,31.14,1,1,31.14,C
2014-03-15,E5,31.00,4,4,31.62,C
2014-03-10,E3,31.46,4,1,31.46,D
2014-03-08,A3,31.79,4,5,32.23,D
2014-03-03,A6,31.20,3,5,31.81,D
2014-03-01,E3,31.61,3,3,31.88,D

【讨论】:

    猜你喜欢
    • 2017-10-10
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多