Selenium find_elements_ 来自标签 [英] Selenium find_elements_ from a tag

查看:25
本文介绍了Selenium find_elements_ 来自标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Booking.com 获取一些酒店信息.该网站提供了一些酒店信息,在这种特殊情况下,还有多少房间可用.下面显示了 Booking.com 网站上的 span 标签,我只想提取所有列出的酒店的 data-x-left-count 的数量.

I want to scrape some hotel information from Booking.com. The website provides some hotel informations, in this particular case, how many rooms are still available. The following shows the span tag from the Booking.com website and i want to extract only the number of data-x-left-count for all listed hotels.

<span class="only_x_left sr_rooms_left_wrap " data-x-left-count="6">
Nur noch 6 Zimmer auf unserer Seite verfügbar!
</span>

我试图通过查找元素并返回一个 selenium 对象数组来处理它.

I tried to approach it by finding the elements and returning an array of selenium objects.

availabilities_element = browser.find_elements_by_xpath("(//span[contains(.,'nur noch')])[2]")

然后是列表推导以获取实际的酒店名称而不是 selenium 对象.

And then a list comprehension to get the actual hotel titles and not the selenium objects.

availabilities = [x.text for x in availabilities_element]

但是我在获取数据方面仍然存在一些问题.我希望得到可用房间的列表(只有数字,仅此而已).有没有一种干净简单的解决方案?

But i have still some problems to get the data. I expect to get a list (just the numbers and nothing more) of the available rooms. Is there a way for a clean simple solution?

推荐答案

假设该属性仅与剩余房间相关联,您可以简单地使用属性选择器

Assuming that attribute is only associated with rooms left you can simply use attribute selector

rooms_left = [item.get_attribute('data-x-left-count') for item in driver.find_elements_by_css_selector("[data-x-left-count]")]

这篇关于Selenium find_elements_ 来自标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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