在一组 html 上迭代调用 html2text 不起作用



这是代码片段:

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文件,而不是正确管道输出。我什至尝试用elinks -dump替换html2text但无济于事。

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

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)

为什么不使用html2text作为 Python 库呢?

h = html2text.HTML2Text()
txt = h.handle(open(infile).read())

最新更新