从网页表中读取列

时间:2017-09-07 14:13:34

标签: python python-3.x web-scraping

我希望能够只阅读"每日太阳辐射 - 水平"来自NASA网页。我该怎么办呢?这是我的代码:

<script>

它只显示完整的表格。

1 个答案:

答案 0 :(得分: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()