2017-09-07 2 views
1

NASA 웹 페이지에서 "Daily solar radiation - horizontal"열만 읽을 수 있기를 원합니다. 어떻게해야합니까? 여기 내 코드는 다음과 같습니다.웹 페이지 테이블에서 열 읽기

# Horizontal radiation values (kwh/m**2) 
import urllib.parse 
import html5lib 
import pandas as pd 

url = "https://eosweb.larc.nasa.gov/cgi-bin/sse/retscreen.cgi?email=rets%40nrcan.gc.ca&step=1&lat=49.4&lon=7.3&submit=Submit" 

params = {'lat':a,'lon':b} 

url_parts = list(urllib.parse.urlparse(url)) 

query = dict(urllib.parse.parse_qsl(url_parts[4])) 

query.update(params) 

url_parts[4] = urllib.parse.urlencode(query) 

print(urllib.parse.urlunparse(url_parts)) 

webresult = pd.read_html(urllib.parse.urlunparse(url_parts)) 
webresult[3] 

전체 테이블 만 표시합니다.

+0

무엇이'a','b' 및'pd'입니까? – Adelin

+0

유효한 코드를 게시하십시오. 작성된 코드가 실행되지 않습니다. – Mike

답변

2

BeautifulSoup을 사용하면 쉽게 수행 할 수 있습니다. 설명은 코드 주석에서 제공됩니다.

import bs4, requests 

def getColumn(url): 
    # get the page 
    resp = requests.get(url) 

    # create a BeautifulSoup object that represents the page 
    # and use lxml parser to parse the html 
    soup = bs4.BeautifulSoup(resp.text, 'lxml') 

    # get all the tables in the page 
    tables= soup.findAll('table') 

    # all data of interest will be collected here 
    data = [] 

    #we only want to process the 4th table, so we store it in table 
    table = tables[3] 

    # for each row in this table, get the 4th column and add it in data 
    for row in table.findAll('tr'): 
     row_data= row.findAll('td') 

     if not row_data: continue #skip empty lists 

     column4= row.findAll('td')[3].string # read the 4th column 

     data.append(column4) 

    # data is in string so we need to convert it to float 

    # discard the first and last two elements in the list (we don't want them) 
    # then convert the remaining from string to float 
    data = [ float(x.strip()) for x in data[1:-2]] 

    return data 


def main(): 
    url= 'https://eosweb.larc.nasa.gov/cgi-bin/sse/retscreen.cgi?email=rets%40nrcan.gc.ca&step=1&lat=49.4&lon=7.3&submit=Submit' 
    lst = getColumn(url) 

    print(lst) 

if __name__ == '__main__': 
    main() 
+0

다른 문제가 있습니다. 난 3.6에서 배열로 URL에서 얻은 데이터를 곱하면됩니다하지만이 typeError 계속 : 'NoneType'개체 subscriptable 않습니다. 어떻게 또는 어떻게해야합니까. – Mfon

+0

@Mfon이 예외 유형은 'Null 개체'로 뺄셈을 할 때 발생합니다. 혼자 해결할 수 없다면 새로운 질문을 만들어야합니다. – Anonta