2017-03-17 12 views
1

私はpythonセレンのバインディングを使用してウェブサイトを削り取ろうとしています。python seleniumはテーブルの内容を取得します

セレンを使用してテーブルのコンテンツを取得したいと考えています。

私は非常にPythonとセレンに新しいですので、私の無知を許してください。

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

あなたのCSSセレクタがありますユニークではありません。ページ上でクリックしようとしているものは何ですか? –

+0

ああ、それは一意である必要があります。私は実際にそれぞれをクリックして、各ページからデータを取得したいと思っています。 –

+0

その場合は、クリックしたいhtmlのすべてのリンクを提供してください –

答えて

1

必要な値を取得するためのコードの下に試してみてください。

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) 

tableclass名前が"opening-hours"ではなく、"opening-times"

を出力することを:

'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' 
+0

セレンの王! :-)あなたは私が以下を達成する方法を知っていますか? - http://stackoverflow.com/questions/42875664/click-on-element-with-classname-selenium –

関連する問題