How to STDIN and STDOUT with PHP and Python to use html2text and get a markdown formated text?

蹲街弑〆低调 提交于 2019-12-08 07:12:59

问题


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).


回答1:


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())


来源:https://stackoverflow.com/questions/4686842/how-to-stdin-and-stdout-with-php-and-python-to-use-html2text-and-get-a-markdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!