How to use Python to include a .htm file in a website
I have a file gather.htm
that is a valid HTML file with header/body
and forms
. If I double-click the file on my desktop, it opens correctly in the web browser, automatically sends data form
(via <SCRIPT LANGUAGE="Javascript">document.forms[2].submit();</SCRIPT>
), and the page refreshes with the requested data.
I want Python to make a call requests.post(url)
using gather.htm
. However, my research and my trail and error have yielded no solution.
How is this achieved?
I've tried things along these lines (based on examples found on the internet). I suspect I am missing something simple here!
myUrl = 'www.somewhere.com' filename='/Users/John/Desktop/gather.htm' f = open (filename) r = requests.post(url=myUrl, data = {'title':'test_file'}, files = {'file':f}) print r.status_code print r.text
and
htmfile = 'file:///Users/John/Desktop/gather.htm' files = {'file':open('gather.htm')} webbrowser.open(url,new=2) response = requests.post(url) print response.text
Note that in the second example above, the call webbrowser.open()
works correctly, but requests.post
does not work.
Everything I've tried seems to fail the same way - the url is opened and the page returns by default. It looks like the website never gets the file gather.htm
.
source to share
Since your request is returning 200 OK
, there is nothing wrong with getting your post request to the server. It's hard to give you an exact answer, but the problem is how the server handles the request. Either your post request is being formatted in a way that the server does not recognize, or the server has not been configured to resolve them. If you are managing the site itself, some additional details will help.
As a final check, try this:
r = requests.post(url=myUrl, data={'title':'test_file', 'file':f})
source to share