简体   繁体   English

是否有使用Beautifulsoup解析表的更多pythonic方式

[英]Is there a more pythonic way of parsing my table using Beautifulsoup

I'm a relative newbie to Python. 我是Python的相对新手。 I've a html page with a table similar to the one below. 我有一个html页,其中的表格与下面的表格类似。 That I would like to parse in and process that data in a neater more pythonic way. 我想以更精巧的Python方式解析和处理该数据。

<table border="1">
    <tr><td><b>Test Results</b></td><td><b>Log File</b></td><td><b>Passes</b></td><td><b>Fails</b></td></tr>
    <tr><td><b>Test suite A</b></td><td><a href="A_logs.html">Logs</a></td><td><b>10</b></td><td><b>0</b></td></tr>
    <tr><td><b>Test suite B</b></td><td><a href="B_logs.html">Logs</a></td><td><b>20</b></td><td><b>0</b></td></tr>
    <tr><td><b>Test suite C</b></td><td><a href="C_logs.html">Logs</a></td><td><b>15</b></td><td><b>0</b></td></tr>
</table>

Using BeautifulSoup I've parsed in the table. 使用BeautifulSoup,我已经在表中进行了解析。

results_table = tables[0] # This will get the first table on the page.
table_rows = my_table.findChildren(['th','tr'])

for i in table_rows:
    text = str(i)
    print( "All rows:: {0}\n".format(text))
    if "Test suite A" in text:
        print( "Test Suite: {0}".format(text))
        # strip out html characters
        list = str(BeautifulSoup(text).findAll( text = True )) 
        # strip out any further stray characters such as [,] 
        list = re.sub("[\'\[\]]", "", list) 
        list = list.split(',') # split my list entries by comma
        print("Test: {0}".format(str(list[0])))
        print("Logs: {0}".format(str(list[1])))
        print("Pass: {0}".format(str(list[3])))
        print("Fail: {0}".format(str(list[4])))

So that's my code that does everything I want it to. 这就是我的代码,可以完成我想要的一切。 I'm just wondering if there is a more pythonic way to do it. 我只是想知道是否还有一种更Python化的方式来做到这一点。 Ignoring the print statements as I plan to put this into its own method passing in the results table and return pass, fail, logs, test. 我打算将其放入自己的方法中并传递到结果表中,然后返回通过,失败,日志和测试,请忽略打印语句。

So.. 所以..

def parseHtml(results_table)
    # split out all rows in my table into a list
    table_rows = my_table.findChildren(['th','tr'])
    for i in table_rows:
        text = str(i)
        if "Test suite A" in text:
            # strip out html characters
            list = str(BeautifulSoup(text).findAll( text = True )) 
            # strip out any further stray characters such as [,] 
            list = re.sub("[\'\[\]]", "", list) 
            # split my list entries by comma
            list = list.split(',') 
     return (list[0],list[1],list[3],list[4])
html="""<table border="1">
    <tr><td><b>Test Results</b></td><td><b>Log File</b></td><td><b>Passes</b></td><td><b>Fails</b></td></tr>
    <tr><td><b>Test suite A</b></td><td><a href="A_logs.html">Logs</a></td><td><b>10</b></td><td><b>0</b></td></tr>
    <tr><td><b>Test suite B</b></td><td><a href="B_logs.html">Logs</a></td><td><b>20</b></td><td><b>0</b></td></tr>
    <tr><td><b>Test suite C</b></td><td><a href="C_logs.html">Logs</a></td><td><b>15</b></td><td><b>0</b></td></tr>
</table>"""


from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
data = soup.find_all("b")  # this will be your table

# ignore Test Result etc.. and get Test suite A ... from each row
data = (data[4:][i:i+3] for i in range(0, len(data[4:]),3))
# get all log file names 
logs = iter(x["href"] for x in soup.find_all("a",href=True))

# unpack each subelement and print the tag text
for a, b, c in data:
    print("Test: {}, Log: {}, Pass: {}, Fail: {}".format(a.text ,next(logs),b.text, c.text))


Test: Test suite A, Log: A_logs.html, Pass: 10, Fail: 0
Test: Test suite B, Log: B_logs.html, Pass: 20, Fail: 0
Test: Test suite C, Log: C_logs.html, Pass: 15, Fail: 0

Don't use list as a variable name as it shadows the python list , if you want to get elements from your sublists from your find_all calls either iterate over or index, don't use re. 不要将list用作变量名,因为它会遮盖python list ,如果您想从find_all调用的子列表中获取元素,请迭代或索引,不要使用re。

In such a situation I would tend to iterate over 'tr' and then 'td' 在这种情况下,我倾向于遍历“ tr”然后再“ td”

bs_table = BeautifulSoup(my_table)
ls_rows = []
for ls_tr in bs_table.findAll('tr'):
  ls_rows.append([td_bloc.text for td_bloc in ls_tr.findAll('td')])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM