Extracting data from HTML table

前端 未结 7 772
迷失自我
迷失自我 2020-12-04 15:41

I am looking for a way to get certain info from HTML in linux shell environment.

This is bit that I\'m interested in :

7条回答
  •  青春惊慌失措
    2020-12-04 16:23

    Here is the top answer, adapted for Python3 compatibility, and improved by stripping whitespace in cells:

    from bs4 import BeautifulSoup
    
    html = """
      
Tests Failures Success Rate Average Time Min Time Max Time
103 24 76.70% 71 ms 0 ms 829 ms
""" soup = BeautifulSoup(s, 'html.parser') table = soup.find("table") # The first tr contains the field names. headings = [th.get_text().strip() for th in table.find("tr").find_all("th")] print(headings) datasets = [] for row in table.find_all("tr")[1:]: dataset = dict(zip(headings, (td.get_text() for td in row.find_all("td")))) datasets.append(dataset) print(datasets)

提交回复
热议问题