2016-09-19 2 views
0

데이터를 추출하려는 대화 형 차트가 두 개있는 website이 있습니다. 파이썬에서 셀레늄 webdriver를 사용하기 전에 몇 가지 웹 스크래퍼를 작성했지만이 문제는 다른 것으로 보입니다. stackoverflow에 대한 몇 가지 유사한 질문을 살펴 봤습니다. 그것들은 솔루션이 json 파일에서 직접 데이터를 다운로드 할 수있는 것으로 보인다. 웹 사이트의 소스 코드를보고 몇 개의 json 파일을 확인했지만, 검사를하면 데이터가 포함되어 있지 않은 것 같습니다.대화 형 그래프에서 데이터 스크랩

그래프에서 데이터를 다운로드하는 방법을 아는 사람이 있습니까? 특히 나는이 막대 차트에 관심이 :

편집 .//*[@id='network_download']

감사 : 나는 방화범을 사용하여 웹 사이트를 검사 할 때 나는 ITIS 가능한 다음과 같은 형식으로 데이터를 가져 오는 것을보고 있음을 추가해야합니다. 그러나 이것은 레이블이 없으므로 분명히 도움이되지 않습니다.

<circle fill="#8CB1AA" cx="713.4318516666667" cy="5.357142857142858" r="4.5" style="opacity: 0.983087;"> 
<circle fill="#8CB1AA" cx="694.1212663333334" cy="10.714285714285715" r="4.5" style="opacity: 0.983087;"> 
<circle fill="#CEA379" cx="626.4726493333333" cy="16.071428571428573" r="4.5" style="opacity: 0.983087;"> 
<circle fill="#B0B359" cx="613.88416" cy="21.42857142857143" r="4.5" style="opacity: 0.983087;"> 
<circle fill="#D1D49E" cx="602.917665" cy="26.785714285714285" r="4.5" style="opacity: 0.983087;"> 
<circle fill="#A5E0B5" cx="581.5437366666666" cy="32.142857142857146" r="4.5" style="opacity: 0.983087;"> 

답변

0

이렇게 SVG 차트는 긁기가 어려울 수 있습니다. 마우스로 개별 요소를 실제로 가리킬 때까지 원하는 숫자가 표시되지 않습니다.

값 툴팁 점

  • 긁어을 (액션 체인), 모든 점 dots_list의 각 점에 대한
  • 의 목록을 찾아 클릭하거나 호버 당신이

    1. 에 필요한 데이터를 얻으려면 즉, 이것은 나를 위해 작동

    팝업 :

    from __future__ import print_function 
    
    from pprint import pprint as pp 
    
    from selenium import webdriver 
    from selenium.webdriver.common.action_chains import ActionChains 
    
    
    def main(): 
        driver = webdriver.Chrome() 
        ac = ActionChains(driver) 
    
        try: 
         driver.get("https://opensignal.com/reports/2016/02/state-of-lte-q4-2015/") 
    
         dots_css = "div#network_download g g.dots_container circle" 
         dots_list = driver.find_elements_by_css_selector(dots_css) 
    
         print("Found {0} data points".format(len(dots_list))) 
    
         download_speeds = list() 
         for index, _ in enumerate(dots_list, 1): 
          # Because this is an SVG chart, and because we need to hover it, 
          # it is very likely that the elements will go stale as we do this. For 
          # that reason we need to require each dot element right before we click it 
          single_dot_css = dots_css + ":nth-child({0})".format(index) 
          dot = driver.find_element_by_css_selector(single_dot_css) 
          dot.click() 
    
          # Scrape the text from the popup 
          popup_css = "div#network_download div.tooltip" 
          popup_text = driver.find_element_by_css_selector(popup_css).text 
          pp(popup_text) 
          rank, comp_and_country, speed = popup_text.split("\n") 
          company, country = comp_and_country.split(" in ") 
          speed_dict = { 
           "rank": rank.split(" Globally")[0].strip("#"), 
           "company": company, 
           "country": country, 
           "speed": speed.split("Download speed: ")[1] 
          } 
          download_speeds.append(speed_dict) 
    
          # Hover away from the tool tip so it clears 
          hover_elem = driver.find_element_by_id("network_download") 
          ac.move_to_element(hover_elem).perform() 
    
         pp(download_speeds) 
    
        finally: 
         driver.quit() 
    
    if __name__ == "__main__": 
        main() 
    

    샘플 출력 : 사람들은 단지 SVG 차트에서 점을 그리는 방법을 지정으로

    (.venv35) ➜ stackoverflow python svg_charts.py 
    Found 182 data points 
    '#1 Globally\nSingTel in Singapore\nDownload speed: 40 Mbps' 
    '#2 Globally\nStarHub in Singapore\nDownload speed: 39 Mbps' 
    '#3 Globally\nSaskTel in Canada\nDownload speed: 35 Mbps' 
    '#4 Globally\nOrange in Israel\nDownload speed: 35 Mbps' 
    '#5 Globally\nolleh in South Korea\nDownload speed: 34 Mbps' 
    '#6 Globally\nVodafone in Romania\nDownload speed: 33 Mbps' 
    '#7 Globally\nVodafone in New Zealand\nDownload speed: 32 Mbps' 
    '#8 Globally\nTDC in Denmark\nDownload speed: 31 Mbps' 
    '#9 Globally\nT-Mobile in Hungary\nDownload speed: 30 Mbps' 
    '#10 Globally\nT-Mobile in Netherlands\nDownload speed: 30 Mbps' 
    '#11 Globally\nM1 in Singapore\nDownload speed: 29 Mbps' 
    '#12 Globally\nTelstra in Australia\nDownload speed: 29 Mbps' 
    '#13 Globally\nTelenor in Hungary\nDownload speed: 29 Mbps' 
    <...> 
    [{'company': 'SingTel', 
        'country': 'Singapore', 
        'rank': '1', 
        'speed': '40 Mbps'}, 
    {'company': 'StarHub', 
        'country': 'Singapore', 
        'rank': '2', 
        'speed': '39 Mbps'}, 
    {'company': 'SaskTel', 'country': 'Canada', 'rank': '3', 'speed': '35 Mbps'} 
    ... 
    ] 
    

    원 요소는 질문에 참조 된 값, 특히 유용하지 않은 것을 주목해야한다.