BeautifulSoup, a dictionary from an HTML table

后端 未结 4 1684
既然无缘
既然无缘 2020-12-05 07:38

I am trying to scrape table data from a website.

Here is a simple example table:

t = \'\' +\\
    \'
4条回答
  •  借酒劲吻你
    2020-12-05 08:34

    BeautifulSoup and Python have evolved, so if someone comes here with newer versions:

    Python>=3.7
    BeautifulSoup>=4.7
    

    Here's updated code that works:

    # import bs4 and create your 'soup' object
    
    table = soup.find('table')
    
    headers = [header.text for header in table.find_all('th')]
    results = [{headers[i]: cell for i, cell in enumerate(row.find_all('td'))}
               for row in table.find_all('tr')]
    

提交回复
热议问题