从html中提取表格数据并另存为文本文件

时间:2018-03-15 11:18:06

标签: html python-2.7 pandas numpy urllib2

我想从html中提取表格数据并保存为text file

import urllib2, numpy as np, pandas as pd
fo = 'fo.txt'
url = 'https://coinmarketcap.com/currencies/bitcoin/historical-data/'
html = urllib2.urlopen(url).read()
rows = pd.read_html(html)
print type(rows)
print rows

for row in rows:
    this_row = "|".join([str(td) for td in row])
    fo.write(this_row + "\n")

但得到了错误:

Traceback (most recent call last):
    fo.write(this_row + "\n")
AttributeError: 'str' object has no attribute 'write'

文本文件中生成的表格数据看起来与原始链接相同: https://coinmarketcap.com/currencies/bitcoin/historical-data/

请帮忙!

1 个答案:

答案 0 :(得分:1)

如果要写入文本文件,则需要文件对象。在您的源代码中,fo对象是string

在python中,你可以打开一个文件来写这样的文字:

with open(fo,'w') as text_file:
    for row in rows:
        this_row = row
        text_file.write(this_row + "\n")