Extracting selected columns from a table using BeautifulSoup

前端 未结 3 1465
情话喂你
情话喂你 2020-12-05 15:48

I am trying to extract the first and third columns of this data table using BeautifulSoup. From looking at the HTML the first column has a tag. The o

3条回答
  •  一生所求
    2020-12-05 16:29

    you can try this code also

    import requests
    from bs4 import BeautifulSoup
    page =requests.get("http://www.samhsa.gov/data/NSDUH/2k10State/NSDUHsae2010/NSDUHsaeAppC2010.htm")
    soup = BeautifulSoup(page.content, 'html.parser')
    for row in soup.findAll('table')[0].tbody.findAll('tr'):
        first_column = row.findAll('th')[0].contents
        third_column = row.findAll('td')[2].contents
        print (first_column, third_column)
    

提交回复
热议问题