how to make python request.get wait a few seconds? -
i wanted experience html crawling, wanted see if grab values of next site: http://www.iex.nl/aandeel-koers/11890/royal-imtech/koers.aspx
this site shows cost of imtech shares. if take @ site, see there 1 number shown in bold, cost of share.
as may have seen, cost changes, , that's okay. want value @ time run script @ point in time.
but if reload page, may notice how first shows "laatste koers" , after delay of 1 sec shows "realtime"
as may have figured out now, i'm interested in "realtime" value.
here question, how value, i've tried time.sleep(2) on different places. i've tried timeout @ request. both didn't work.
how can prepare this?
from lxml import html import requests pagina = 'http://www.iex.nl/aandeel-koers/11890/royal-imtech/koers.aspx' page = requests.get(pagina) tree = html.fromstring(page.text) koers = tree.xpath('//span[@class="realtimelabel"]/text()') prices = tree.xpath('//span[@id="ctl00_ctl00_content_leftcontent_pricedetails_lbllastprice"]/text()') print koers[0], pagina.split("/")[5], prices[0]
i output this
laatste koers royal-imtech 0,093
while want output this
realtime royal-imtech 0,093
i suggest utilize wait until element changes.
find block of code below help you.
def wait_while(condition, timeout, delta=1): """ @condition: lambda function checks if text contains "realtime" @timeout: max waiting time @delta: time after check has made """ max_time = time.time() + timeout while max_time > time.time(): if condition(): homecoming true time.sleep(delta) homecoming false
python python-2.7 lxml python-requests
No comments:
Post a Comment