1

I'm trying to scrape the data behind this graph (when hovering over the bars (e.g. 2018, 78.16). Option 1: Python & BS: I'm familiar with these libraries, however when inspecting I can't find the data
Option 2: Look for JSON file: Again, no luck. There does not seem to be a JSON datafile

Does anyone of you have an idea? (sorry for the open question, but the above two options are the only ones I'm aware of.

https://www.statista.com/statistics/264911/dells-net-revenue-since-1996/

Many thanks!

Sibren De Preter
  • 109
  • 1
  • 13
  • It would help if you give a link to the site you're trying to scrape from and are more specific about which tags you're interested in. If the content is dynamically generated through JavaScript, you might need to use something like Selenium in combination with BeautifulSoup to scrape it. – Mihai Chelaru May 09 '18 at 14:14
  • Sorry, forgot the link! Many thanks :) – Sibren De Preter May 09 '18 at 14:51

1 Answers1

1

Looking at the page source, it turns out that the data in that graph is contained in a table element somewhere on the page. You can get the data you're looking for using Selenium and BeautifulSoup as below:

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.statista.com/statistics/264911/dells-net-revenue-since-1996/")

html = driver.page_source
soup = BeautifulSoup(html, "lxml")

chart = soup.find("tbody", {"role" : "alert"})
children = chart.find_all("tr")

data = []
for tag in children:
    data_tuple = (tag.text[:3],tag.text[3:])
    data.append(data_tuple)

print(data)

This produces the following output:

[("'96", '5.3'), ("'97", '7.8'), ("'98", '12.3'), ("'99", '18.2'), ("'00", '25.3'), ("'01", '31.9'), ("'02", '31.2'), ("'03", '35.3'), ("'04", '41.3'), ("'05", '49.1'), ("'06", '55.8'), ("'07", '57.4'), ("'08", '61.1'), ("'09", '61.1'), ("'10", '52.9'), ("'11", '61.5'), ("'12", '62.1'), ("'13", '56.9'), ("'14", '55.58'), ("'15", '54.1'), ("'16", '50.9'), ("'17", '61.6'), ("'18", '78.66')]

If you are still interested in scraping the tooltips from the chart, you might have to look at something like this where you'd have to get it to click on each of the elements so that the popup tooltip can be generated, then scrape the information as it's displayed.

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51