2017-10-07 3 views
-3

Ich bin neu bei Python und ich brauche Hilfe mit Web Scraping-Code, um eine dynamische Karte jede Woche zu speichern. Dies ist die site Ich interessiere mich für. Der Zweck ist es, auf die Seite zu gelangen, wählen Sie Saison, wählen Sie Woche, und laden Sie das Bild in einen lokalen Ordner. Ich verwende das Bild zur Integration für einen automatisierten wöchentlichen Bericht mit SAS.Web Scraping mit Python, mit Navigation Controller

danke im voraus!

+1

Welche Art von Hilfe benötigen Sie? –

+0

Ich brauche Hilfe mit dem Code ... so kann ich es auf Spyder ausführen, um die .png-Datei zu speichern. – Arthuro

+0

Was hast du bisher versucht? Sie sollten eine Art Codebeispiel angeben, in dem angegeben ist, wo genau Sie Probleme haben. Ohne dass Sie hier keine Hilfe bekommen. –

Antwort

0
import sys 
import os 
import time 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile 
from selenium import webdriver 
import arrow 

BASE_URL = 'https://gis.cdc.gov/grasp/fluview/main.html' 
DOWNLOAD_PATH = "/Users/" 

def closeWebDriver(driver): 

    if os.name == 'nt': 
     driver.quit() 
    else: 
     driver.close() 

def getImage(): 

    profile = FirefoxProfile() 

    profile.set_preference("browser.download.panel.shown", False) 
    profile.set_preference("browser.helperApps.neverAsk.openFile","image/png") 
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "image/png") 
    profile.set_preference("browser.download.folderList", 2); 
    profile.set_preference("browser.download.dir", DOWNLOAD_PATH) 

    driver = webdriver.Firefox(firefox_profile=profile) 

    driver.get(BASE_URL) 

    time.sleep(5) 

    if not isValidTimeFrame(driver): 
     print('Not the time to download yet!') 
     closeWebDriver(driver) 
     return 

    selectFirstWeek(driver) 

    print('- Consume the web.') 
    wrapper = driver.find_element_by_class_name('downloads-help-area') 

    download_img_els = wrapper.find_elements_by_class_name('downloads-button') 

    for el in download_img_els: 
     text = el.text.encode('utf-8') 
#  print(text) 
     if 'download image' == text.strip().lower(): 
      # Process 
      downloadImage(el) 
      break 

    time.sleep(5) 
    closeWebDriver(driver) 


def isValidTimeFrame(driver): 
    seasons_button = driver.find_element_by_class_name('seasons-button') 
    time_frame = seasons_button.text.encode('utf-8').strip().lower() 
    current_year = arrow.now().to('local') 
    current_year_str = current_year.format('YYYY') 
    next_year = current_year.shift(years=1) 
    next_year_str = next_year.format('YY') 
    print(time_frame) 
    compare_year = '%s-%s' % (current_year_str, next_year_str) 

    return time_frame == compare_year 

def selectFirstWeek(driver): 
    prev = driver.find_element_by_id('prevMap') 
    week = driver.find_element_by_id('weekSlider') 

    while True: 
     print(week) 
     current_number = week.get_property('value') 
     print('- Week: ' + current_number) 
     prev.click() 
     if int(current_number) < 2: 
      break; 

    time.sleep(1) 


def downloadImage(el): 
    print('- Click on ' + el.text) 
    el.click() 


getImage()