简体   繁体   中英

Dynamic Table Scraping with selenium in python

I am trying to access data at this site: http://surge.srcc.lsu.edu/s1.html . So far I have my code loop through the two drop down menus, but the table is dynamically named and I am having trouble getting the data from it. I was trying to access the data through the class above "output_data_table" but was having trouble.

# importing libraries
from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
import lxml.html



driver = webdriver.Firefox()
driver.get("http://surge.srcc.lsu.edu/s1.html")

# definition for switching frames
def frame_switch(css_selector):
  driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))  

frame_switch("iframe")

html_source = driver.page_source
nameSelect = Select(driver.find_element_by_xpath('//select[@id="storm_name"]'))
stormCount = len(nameSelect.options)

for i in range(1, stormCount):
    print("starting loop on option storm " + nameSelect.options[i].text)
    nameSelect.select_by_index(i)
    time.sleep(3)


    yearSelect = Select(driver.find_element_by_xpath('//select[@id="year"]'))
    yearCount = len(yearSelect.options)
    for j in range(1, yearCount):
        print("starting loop on option year " + yearSelect.options[j].text)
        yearSelect.select_by_index(j)


        root = lxml.html.fromstring(driver.page_source)

        #table=driver.find_element_by_id("output_data_table")

        for row in root.xpath('.//table[@id="output_data_table"]//tr'):
        # needs dynamic table name
            cells = row.xpath('.//td/text()')
            dict_value = {'0th': cells[0],
                  '1st': cells[1],
                  '2nd': cells[2],
                  '3rd': cells[3],
                  '4th': cells[5],
                  '5th': cells[6],
                  '6th': cells[7],
                  '7th': cells[8]}
            print(dict_value)

Seems like you have to wait before calling "root = lxml.html.fromstring(driver.page_source)".

If you don't wait you get the html source without the table being generated by javascript. Put a "time.sleep(10)" before it.

This seems to get the table. I have used BeautifulSoup for a quick example.

from selenium import webdriver
import time, re
from selenium.webdriver.support.ui import Select
import lxml.html
from bs4 import BeautifulSoup

driver = webdriver.Firefox()
driver.get("http://surge.srcc.lsu.edu/s1.html")

# definition for switching frames
def frame_switch(css_selector):
  driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))

frame_switch("iframe")

html_source = driver.page_source

nameSelect = Select(driver.find_element_by_xpath('//select[@id="storm_name"]'))
stormCount = len(nameSelect.options)

for i in range(1, stormCount):
    print("starting loop on option storm " + nameSelect.options[i].text)
    nameSelect.select_by_index(i)
    time.sleep(3)


    yearSelect = Select(driver.find_element_by_xpath('//select[@id="year"]'))
    yearCount = len(yearSelect.options)
    for j in range(1, yearCount):
        print("starting loop on option year " + yearSelect.options[j].text)
        yearSelect.select_by_index(j)


        time.sleep(10)

        soup = BeautifulSoup(driver.page_source, 'html.parser')

        # get the needed table body
        print soup.find_all("tbody", {"class" : re.compile(".*")})[1].prettify()


        # print out each column

        get_table = soup.find_all("tbody", {"class" : re.compile(".*")})[1]
        columns = get_table.find_all("tr")

        for column in columns:
           print column.getText()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM