Selenium find_elements_ from a tag

独自空忆成欢 提交于 2019-12-20 07:39:48

问题


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>

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]")

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?


回答1:


Welcome to SO. Here is the simple approach to get the number of vacant rooms.

# get all the vacant room elements
rooms = driver.find_elements_by_xpath("//span[@class='only_x_left sr_rooms_left_wrap ']")
for room in rooms:
    # get the number of elements
    print(room.get_attribute('data-x-left-count'))



回答2:


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]")]


来源:https://stackoverflow.com/questions/55977873/selenium-find-elements-from-a-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!