python selenium get content of table

天涯浪子 提交于 2021-02-10 16:11:13

问题


I am trying to scrape a website using python selenium bindings.

I want to get the content of a table using selenium.

I am quite new to python and selenium so please excuse my ignorance.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://www.designmynight.com/london/bars/soho/six-storeys')

hours = driver.find_element_by_xpath('//li[@id="hours"]')

driver.find_element_by_xpath('//li[@id="hours"]').click()

hoursTable = driver.find_elements_by_css_selector("table.opening-hours")

print hoursTable

回答1:


Try below code to get required values:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://www.designmynight.com/london/bars/soho/six-storeys')

hours = driver.find_element_by_xpath('//li[@id="hours"]')
hours.click()

hoursTable = driver.find_elements_by_css_selector("table.opening-times tr")
for row in hoursTable:
    print(row.text)

Note that class name of table is not "opening-hours", but "opening-times"

Output:

'Day Open Close Notes'
'Monday 08:00 00:00'
'Tuesday 08:00 00:00'
'Wednesday 08:00 00:00'
'Thursday 08:00 01:00'
'Friday (today) 08:00 02:00'
'Saturday 10:00 02:00'
'Sunday 10:00 00:00'


来源:https://stackoverflow.com/questions/42856915/python-selenium-get-content-of-table

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