0

这是代码片段:

for i in obj:
    url = "someurl" + i
    oars = requests.get(url, timeout=1)
    soup = BeautifulSoup(oars.content)
    fout = open(i + ".html", "wt")
    print((type(soup.prettify)))
    fout.write(oars.text)
    oars.close
    #fout.write(soup.get_text())
    # Still not working, using zsh for now
    if call("html2text " + i + ".html" + ">" + i + ".txt", shell=True) == 0:
        print("yay")
        #call("rm -f " + i + ".html", shell=True)
    else:
        print(i)

但是 html2text 只是创建空的 txt 文件,而不是正确地管道输出。我什至尝试替换html2textelinks -dump但无济于事。

4

2 回答 2

0

为什么不用html2text作 Python 库?

h = html2text.HTML2Text()
txt = h.handle(open(infile).read())
于 2013-11-06T13:45:46.913 回答
0

不确定,但这可能是你所追求的

import subprocess
import sys

outfile = i + ".txt"


cmd = sys.path[0] + "/htmltotext " + i + ".html"

with open(outfile, "w") as output_f:
    p = subprocess.Popen(cmd, stdout=output_f, shell=True)
于 2013-10-31T10:21:07.293 回答