1

I want to scrape the data on the website https://www.climatechangecommunication.org/climate-change-opinion-map/. I am somewhat familiar with selenium. But the data I need which is below the map and the tooltip on the map is not visible in the source file. I have read some posts about using PhantomJS and others. However, I am not sure where and how to start. Can someone please help get me started.

Thanks, Rexon

3
  • Not sure what you mean by "below the map", what specifically? As for the tooltip, that appears on hover. The data that populates the tooltip is probably in the source file in a different format though. Commented Jul 13, 2018 at 2:52
  • All the horizontal bars below the map with percentages Commented Jul 13, 2018 at 3:00
  • Rather than scraping it, why not download it directly? I noticed that the link at the bottom of the page you provided refers to the Yale Climate Opinion map. Googled that and came up with a page where you can download the data Commented Jul 13, 2018 at 11:30

1 Answer 1

1

You can use this sample code:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.climatechangecommunication.org/climate-change-opinion-map/")

# switch to iframe
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@src = 'https://environment.yale.edu/ycom/factsheets/MapPage/2017Rev/?est=happening&type=value&geo=county']")))

# do your stuff
united_states = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='document']/div[4]//*[name()='svg']")))
print(united_states.text)

# switch back to default content
driver.switch_to.default_content()

Output:

50%
No
12%
Yes
70%
United States

Screenshot of the element:

img

Explanantion: first of all, to be able to interact with elements below the map you have to switch to iframe content, otherwise it is not possible to interact with this elements. Then the data below the map is in svg tags, which are also not trivial. To be able to do this, you the sample I have provided.

PS: I have used WebDriverWait in my code. With WebDriverWait your code becomes quickier and stable, since Selenium waits for particular conditions like visibility or clickable of particular element. In the sample code the driver wait at least 10 seconds until expected condition will be satisfied.

Sign up to request clarification or add additional context in comments.

2 Comments

Would you know how I can access the source code within the iframe. I want to pass states and countys in the drop down menus. I can get the list of all the states and countys but when I use click() nothing happens. Thanks in advance.
You can create a new question with details and send a link to it me. I'll try help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.