Python requests: post JavaScript commands
Question:
I’m trying to get the content of a site, but this content only appears when I click the ‘Chart’ link, which is a JavaScript command like this:
javascript:__doPostBack('fcc1$dgrC$ctl02$ctl00',''
from a element like this:
<a href="javascript:__doPostBack('fcc1$dgrC$ctl02$ctl00','')">Chart</a>
And this is what happens when you click on ‘Chart’:
You can see that the link doesn’t change, so the only way to get the chart code is by somehow send that JavaScript command
How to find all the chart links?
How can I do a ‘virtual click’ on the chart links?
And after that, how to get the new html?
I really appreciate your help!
Answers:
If those links are indeed “clicked” by javascript listeners, you may have trouble finding a real link to click.
Instead, you could use a tool to simulate a web browser in python and “click” on that link to get to the next page. The best one I’ve used is Mechanical Soup: https://mechanicalsoup.readthedocs.io/en/stable/introduction.html.
doPostBack just sets the __EVENTTARGET and __EVENTARGUMENT form fields and submits the form. Those '’s are just quotes BTW.
I’m trying to get the content of a site, but this content only appears when I click the ‘Chart’ link, which is a JavaScript command like this:
javascript:__doPostBack('fcc1$dgrC$ctl02$ctl00',''
from a element like this:
<a href="javascript:__doPostBack('fcc1$dgrC$ctl02$ctl00','')">Chart</a>
And this is what happens when you click on ‘Chart’:
You can see that the link doesn’t change, so the only way to get the chart code is by somehow send that JavaScript command
How to find all the chart links?
How can I do a ‘virtual click’ on the chart links?
And after that, how to get the new html?
I really appreciate your help!
If those links are indeed “clicked” by javascript listeners, you may have trouble finding a real link to click.
Instead, you could use a tool to simulate a web browser in python and “click” on that link to get to the next page. The best one I’ve used is Mechanical Soup: https://mechanicalsoup.readthedocs.io/en/stable/introduction.html.
doPostBack just sets the __EVENTTARGET and __EVENTARGUMENT form fields and submits the form. Those '’s are just quotes BTW.