Selenium find_elements_ from a tag

2020-05-09 19:30发布

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?

2条回答
成全新的幸福
2楼-- · 2020-05-09 19:44

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]")]
查看更多
甜甜的少女心
3楼-- · 2020-05-09 19:47

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'))
查看更多
登录 后发表回答