I am trying to scrap this website for fund price history, by providing the start date, end date and click the 'Get Prices' button via POST method.
However the page requests.post() return does not contain the results, as if the "Get Price" button was never pressed. This is the URL I put together through the code:
https://www.nysaves.org/nytpl/fundperform/fundHistory.do?submit=Get+Prices&endDate=02%2F20%2F2016&fundid=1003022&startDate=01%2F01%2F2016
I read other posts on Stackoverflow about submitting form via POST in Python and I couldn't make it work. Could you please help? Many thanks!
import requests
import datetime
startDate = datetime.datetime(2016,1,1).strftime('%m/%d/%Y')
endDate = datetime.datetime(2016,2,20).strftime('%m/%d/%Y')
serviceurl = 'https://www.nysaves.org/nytpl/fundperform/fundHistory.do?'
payload = {'fundid':1003022, 'startDate':startDate, 'endDate': endDate, 'submit':'Get Prices'}
r = requests.post(serviceurl, params=payload)
#from IPython.core.display import HTML
#HTML(r.content.decode('utf-8'))
In javascript onclick event , you can use form. submit() method to submit form. You can perform submit action by, submit button, by clicking on hyperlink, button and image tag etc. You can also perform javascript form submission by form attributes like id, name, class, tag name as well.
The :submit selector selects button and input elements with type=submit. If a button element has no defined type, most browsers will use it as a button with type=submit. Tip: Using input:submit as a selector will not select the button element.
You can tie a submit button to a form that the button doesn't live inside of. The trick is to give the form an id and then reference that id with the button's form property. With this setup, clicking the Submit button will cause the form to be submitted.
In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.
https://www.nysaves.org/nytpl/fundperform/fundHistorySearch.do
, not https://www.nysaves.org/nytpl/fundperform/fundHistory.do?
.submit
keyword. But adding it won't hurt.This code will work:
startDate = datetime.datetime(2016,1,1).strftime('%m/%d/%Y')
endDate = datetime.datetime(2016,2,20).strftime('%m/%d/%Y')
serviceurl = 'https://www.nysaves.org/nytpl/fundperform/fundHistorySearch.do'
payload = {'fundid': 1003022, 'startDate': startDate, 'endDate': endDate}
r = requests.post(serviceurl, data=payload)
print(r.text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With