如何使python request.get等待几秒钟? [英] how to make python request.get wait a few seconds?

查看:2555
本文介绍了如何使python request.get等待几秒钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一些有关html爬网的经验,所以我想看看是否可以获取以下网站的一些价值:

I wanted to do get some experience with html crawling, so I wanted to see if I could grab some values of the following site: http://www.iex.nl/Aandeel-Koers/11890/Royal-Imtech/koers.aspx

此网站显示了imtech股票的价格. 如果您查看该站点,会看到以粗体显示1个数字,这是股份的价格.

This site shows the price of imtech shares. If you take a look at the site, you see there is 1 number shown in bold, this is the price of the share.

您可能已经看到,这个价格会发生变化,这没关系.我只想在此时运行脚本时获得该值.

As you may have seen, this price changes, and that's okay. I only want the value at the time I run my script at this point in time.

但是如果您重新加载页面,您可能会注意到它首先显示为"laatste koers",并在延迟1秒后显示为实时"

but if you reload the page, you may notice how it first shows "laatste koers" and after a delay of 1 second it shows "realtime"

您可能已经知道了,我对实时"值很感兴趣.

As you may have figured out by now, I'm interested in the "realtime" value.

这是我的问题,我如何在不同地方尝试过time.sleep(2)来获得此值.我已尝试根据请求超时.两者都不起作用.

Here is my question, how do I get this value, I've tried time.sleep(2) on different places. I've tried a timeout at the request. Both didn't work.

我该如何解决?

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]

我得到这样的输出

Laatste koers Royal-Imtech 0,093

当我想要这样的输出

Realtime Royal-Imtech 0,093

推荐答案

我建议等待元素更改.

找到下面的代码块以帮助您.

Find the block of code below to help you.

def wait_while(condition, timeout, delta=1):
    """
    @condition: lambda function which checks if the text contains "REALTIME"
    @timeout: Max waiting time
    @delta: time after which another check has to be made
    """
    max_time = time.time() + timeout
    while max_time > time.time():
        if condition():
            return True
        time.sleep(delta)
    return False

这篇关于如何使python request.get等待几秒钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆