【问题标题】:How to STDIN and STDOUT with PHP and Python to use html2text and get a markdown formated text?如何使用 PHP 和 Python 进行 STDIN 和 STDOUT 以使用 html2text 并获取 markdown 格式的文本?
【发布时间】:2011-01-14 00:21:25
【问题描述】:

我通过 STDIN 将 HTML 文本从 PHP 发送到 Python。我的目标是使用 Aaron Swartz 的脚本“html2text.py”并通过 STDOUT 将结果打印到 PHP。

Camarade Jan 给了我这个词,让我朝着正确的方向前进。这是我的测试:

PHP 代码:

$t='<p><b>Hello</b><i>world!</i></p>';
$scaped=preg_quote($t,"/")."\n";//\<p\>\<b\>Hello\<\/b\>\<i\>world\!\<\/i\>\<\/p\>
exec('python hi.py '.$scaped,$r);
print_r($r);//result

Python 代码:

#! /usr/bin/env python
import html2text
import sys
#print html2text.html2text(sys.stdin.read()) #this part of the code didn't work out...
print html2text.html2text(sys.argv[1])

结果:

Array
(
    [0] => **Hello**_world!_
    [1] => 
    [2] => 
)

所有文件都在同一个目录中(在 chmod 077 下)。我正在使用 Aaron Swartz 的 html2text.py 版本 2.39,并且还在我的 Fedora 14 上安装了“python-html2text.noarch”(尽管我无法使用最后一个)。

【问题讨论】:

  • 能不能也加上你正在使用的命令行?

标签: php python html markdown text-files


【解决方案1】:

您只是将最后一行传递给 html2text ,而您没有正确使用 html2text 来代替:

import html2text
import sys

print html2text.html2text(sys.stdin.read())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多