4
# ./scrape.py
from lxml import html
import requests

url = "http://www.my-target-url.com"

page = requests.get(url)
# can I insert some js event codes to execute here?
tree = html.fromstring(page.content)
print tree.xpath("/html/to/target/data/text()")[0]

I made this for scraping target page which has several buttons to change data. I want all the data which can be derived from those buttons. I searched for the way to send POST or GET with parameters. There seems no other way but to trigger js event from that web page though(like I can do in chrome developer's console).

Is there any way I can execute js events to change response object for needed data in this python code? should I use another library other than requests? or some other way which I can search for?(like make some web browser object behind and do with it? then what can help?)

1 Answer 1

1

Short answer, no.

All the javascript events are handled by the browser's js engine. This means you will need a javascript engine too to handle and execute the scripts and trigger the events.

Sign up to request clarification or add additional context in comments.

5 Comments

Could you recommend something that can work like you said? any useful js or python library?
Use phantomjs or selenium-webdriver
I don't think there is an easy way of doing this. Since you want to create a scraper and i'm assuming that it will work in background, maybe you can find what requests are made when you click a button and do the same requests with the same POST/GET params in your script.
Thank you all! I could have a glimpse of the background system. I first tried phantomjs but it was very slow and sometimes not working even example codes(I don't know why untill now) but selenium was great. It must have decreased my wasteful time if I knew it eariler. Selenium is very easy to use and understand. Thank you @theideasmith.
This is an interesting problem that should be solved with a well tested DSL - and in fact it is. Check out Wombat, felipecsl/wombat · GitHub github.com/felipecsl/wombat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.