I am trying to build this script here which will accept a tracking number as an input, build the URL and then get the HTML response. I am trying to display this response in the terminal using the html2text program. I am trying to emulate the command "html2text filename" which is typed in the terminal into my python script, however the raw HTML file is displayed instead of the standard html2text output. where am i going wrong here ?
#!/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)
Update: I have got around the issue currently but storing the output of r.text in a file and then calling it with html2text
The version of html2text
you're using expects the argument to be a filename, not the HTML. To provide the HTML to it, you need to run the command with no argument, and provide the HTML on its standard input.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.