简体   繁体   中英

Run Javascript with Python (onclick)

How could I run a javascript which I found in a html tag with python?

There is a tag in a html which contains javascript which is being run after click (onclick).

Here is the example:

<span id="Handy2">
    <span class="cust-type">0179... </span>
    <a id="dspphone2" style="cursor: pointer;" class="t-info" onclick="jQuery( Handy2 ).load( '/ajax/detail/displayphonenumber.php?coded=MDE3OS8gNjk2MTA5Mw--&amp;adno=140305860&amp;adsource=quoka+online&amp;catid=36_3210' )" data-trackl="Detail 36_3210">
        anzeigen
    </a>
</span>

What should I do to run it and recieve the code which is visible after click on this button?

I'm using mostly BeautifulSoup.

EDIT: I know how to do that using Selenium but it is quite an overkill I think.

You can't just run it in Python as it is already mentioned above, you need a JavaScript interpreter. As it seems to me that you just want to get the data that is loaded with this AJAX call you should do something like this (this is just a sample)

import urllib2
html = urllib2.urlopen('http://your_site_name.com/ajax/detail/displayphonenumber.php?coded=MDE3OS8gNjk2MTA5Mw--&adno=140305860&adsource=quoka+online&catid=36_3210').read()
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)

This is just a small XHR Request, which returns in its response the phone number you are looking for. Try it yourself: http://www.xxx.de/ajax/detail/displayphonenumber.php?coded=MDE3OS8gNjk2MTA5Mw--&adno=140305860&adsource=quoka+online&catid=36_3210 I would use xpath selectors and regex for getting the values you search. Then replace the ';' with '&' and request the new url.

It is not possible to run the JavaScript from python direct.
The best you can do is run selenium and phantomJs for scrapping data.

below link might help you. https://realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM