[英]i am trying to use subprocess module to run a terminal command which consists of an html file
我正在嘗試在此處構建此腳本,該腳本將接受跟蹤號作為輸入,構建URL,然后獲取HTML響應。 我正在嘗試使用html2text程序在終端中顯示此響應。 我正在嘗試模擬在終端中鍵入到我的python腳本中的命令“ html2text filename”,但是顯示的是原始HTML文件,而不是標准的html2text輸出。 我在哪里錯了?
#!/usr/bin/python3
#trial using bash calls no html2text library
import requests
import subprocess # to execute bash commands
try:
check_for_package = subprocess.Popen(("dpkg","-s","html2text"), stdout=subprocess.PIPE)
output = subprocess.check_output(("grep", "Status"), stdin=check_for_package.stdout)
check_for_package.wait()
opstr=str(output, 'utf-8')
print(opstr)
if opstr == "Status: install ok installed\n" :
print("Package installed")
except:
print("installing html2text..............................")
install_pkg = subprocess.check_call("sudo apt install html2text", shell=True)
r = requests.get("http://ipsweb.ptcmysore.gov.in/ipswebtracking/IPSWeb_item_events.asp?itemid=RT404715658HK&Submit=Submit")
print(r.status_code)
raw_html=r.text
#print(raw_html)
#raw_html = str(raw_html , 'utf-8')
view_html = subprocess.Popen(["html2text", raw_html])
output = view_html.communicate()
view_html.wait()
#view_html = subprocess.Popen("html2text template", shell=True)
print(output)
更新:我現在解決了這個問題,但是將r.text的輸出存儲在文件中,然后使用html2text對其進行調用
您使用的html2text
版本期望參數是文件名,而不是HTML。 要為其提供HTML,您需要不帶任何參數地運行命令,並在其標准輸入上提供HTML。
view_html = subprocess.Popen(["html2text"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
view_html.stdin.write(raw_html)
view_html.stdin.close() # Close the pipe so html2text will get EOF
output = view_html.stdout.read()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.