GBR24 GBR24 - 1 year ago 227
Python Question

Click button, then scrape data on seemingly static webpage?

I'm trying to scrape the player statistics in the

table at this link: It's much more difficult to scrape the data as-is when you first appear on that site, so you have the option of clicking 'CSV' right above the table. This format would be much easier to digest.

I'm having trouble

import urllib2
from bs4 import BeautifulSoup
from selenium import webdriver

player_link = ""

browser = webdriver.Firefox()
elem = browser.find_element_by_xpath("//span[@class='tooltip' and @onlick='table2csv('totals')']")

When I run this, a Firefox window pops up, but the code never changes the table from its original format to CSV. The CSV table only pops up in the source code after I click CSV (obviously). How can I get
to click that CSV button and then BS to scrape the data?

Answer Source

You don't need BeautifulSoup here. Click the CSV button with selenium, extract the contents of the appeared pre element with CSV data and parse it with built-in csv module:

import csv
from StringIO import StringIO

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from import By
from import WebDriverWait
from import expected_conditions as EC

player_link = ""

browser = webdriver.Firefox()
wait = WebDriverWait(browser, 10)

# stop load after a timeout
except TimeoutException:

# click "CSV"
elem = wait.until(EC.presence_of_element_located((By.XPATH,  "//div[@class='table_heading']//span[. = 'CSV']")))

# get CSV data
csv_data = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "pre#csv_totals"))).text.encode("utf-8")

# read CSV
reader = csv.reader(StringIO(csv_data))
for line in reader:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download