如何使用PHP和Python进行STDIN和STDOUT以使用html2text并获得markdown格式文本? [英] How to STDIN and STDOUT with PHP and Python to use html2text and get a markdown formated text?

查看:103
本文介绍了如何使用PHP和Python进行STDIN和STDOUT以使用html2text并获得markdown格式文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Camarade Jan给了我这个词,让我走向正确的方向。这里是我的测试:

PHP代码:

  $ t =' < p为H.;< b取代;你好< / b>< I>!世界< / I>< / p为H.'; 
$ scaped = preg_quote($ t,/)。\\\
; // \< p \> \< b\> Hello \< \ / b\> \< i\> world\ \<!\ / i\> \< \ / p\>
exec('python hi.py'。$ scaped,$ r);
print_r($ r); //结果

Python代码:

 #! / usr / bin / env python 
import html2text
import sys
#print html2text.html2text(sys.stdin.read())#这部分代码没有工作出来.. 。
print html2text.html2text(sys.argv [1])$ ​​b $ b

结果:数组

[0] => ** Hello ** _ world!_


  [1] => 
[2] =>

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

解决方案

您只是将最后一行传递给html2text,而您没有正确使用html2text,请改为:

  import html2text 
import sys

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


I am sending an HTML text from PHP to Python via STDIN. My objctive is to use Aaron Swartz's script "html2text.py" and to print the result to PHP via STDOUT.

Camarade Jan gave me the word and put me in the right direction. Here's my test:

PHP code:

$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 code:

#! /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])

Result:

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

All the files are in the same directory (under chmod 077). I am using Aaron Swartz's html2text.py version 2.39 and also installed "python-html2text.noarch" on my Fedora 14 (though I couldn't make it work with this last one).

解决方案

You're just passing the last line to html2text , and you are not using html2text correctly do this instead :

import html2text
import sys

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

这篇关于如何使用PHP和Python进行STDIN和STDOUT以使用html2text并获得markdown格式文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆