提取整个html后,我们如何选择子元素tbody?

时间:2019-06-19 08:13:21

标签: python python-3.x html-table beautifulsoup

我仍然是试图学习beautifulsoup的Python新手。我查看了堆栈上的解决方案,但未成功,请帮助我更好地理解这一点。 我已经提取了如下所示的html

<table cellspacing="0" id="ContentPlaceHolder1_dlDetails" 
     style="width:100%;border-collapse:collapse;">
     <tbody><tr>
     <td>
     <table border="0" cellpadding="5" cellspacing="0" width="70%">
     <tbody><tr>
     <td> </td>
     <td> </td>
     </tr>
     <tr>
     <td bgcolor="#4F95FF" class="listhead" width="49%">Location:</td>
     <td bgcolor="#4F95FF" class="listhead" width="51%">On Site </td>
     </tr>
     <tr>
     <td class="listmaintext">ATM ID: </td>
     <td class="listmaintext">DAGR00401111111</td>
     </tr>
     <tr>
     <td class="listmaintext">ATM Centre:</td>
     <td class="listmaintext"></td>
     </tr>
     <tr>
     <td class="listmaintext">Site Location: </td>
     <td class="listmaintext">ADA Building - Agra</td>
     </tr>

我尝试解析find_all('tbody'),但未成功

        #table = bs.find("table", {"id": "ContentPlaceHolder1_dlDetails"})
        html = browser.page_source
        soup = bs(html, "lxml")
        table = soup.find_all('table', {'id':'ContentPlaceHolder1_dlDetails'})
        table_body = table.find('tbody')
        rows = table.select('tr')
        for row in rows:
            cols = row.find_all('td')
            cols = [ele.text.strip() for ele in cols]
            data.append([ele for ele in cols if ele])values

我正在尝试将值保存在“ listmaintext”类中

错误消息 AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

2 个答案:

答案 0 :(得分:0)

from bs4 import BeautifulSoup

data = '''<table cellspacing="0" id="ContentPlaceHolder1_dlDetails"
     style="width:100%;border-collapse:collapse;">
     <tbody><tr>
     <td>
     <table border="0" cellpadding="5" cellspacing="0" width="70%">
     <tbody><tr>
     <td> </td>
     <td> </td>
     </tr>
     <tr>
     <td bgcolor="#4F95FF" class="listhead" width="49%">Location:</td>
     <td bgcolor="#4F95FF" class="listhead" width="51%">On Site </td>
     </tr>
     <tr>
     <td class="listmaintext">ATM ID: </td>
     <td class="listmaintext">DAGR00401111111</td>
     </tr>
     <tr>
     <td class="listmaintext">ATM Centre:</td>
     <td class="listmaintext"></td>
     </tr>
     <tr>
     <td class="listmaintext">Site Location: </td>
     <td class="listmaintext">ADA Building - Agra</td>
     </tr>'''

soup = BeautifulSoup(data, 'lxml')

s = soup.select('.listmaintext')
for td1, td2 in zip(s[::2], s[1::2]):
    print('{} [{}]'.format(td1.text.strip(), td2.text.strip()))

打印:

ATM ID: [DAGR00401111111]
ATM Centre: []
Site Location: [ADA Building - Agra]

答案 1 :(得分:0)

使用next_sibling

的另一种方法
from bs4 import BeautifulSoup as bs

html ='''
<html>
<table cellspacing="0" id="ContentPlaceHolder1_dlDetails" 
     style="width:100%;border-collapse:collapse;">
     <tbody><tr>
     <td>
     <table border="0" cellpadding="5" cellspacing="0" width="70%">
     <tbody><tr>
     <td> </td>
     <td> </td>
     </tr>
     <tr>
     <td bgcolor="#4F95FF" class="listhead" width="49%">Location:</td>
     <td bgcolor="#4F95FF" class="listhead" width="51%">On Site </td>
     </tr>
     <tr>
     <td class="listmaintext">ATM ID: </td>
     <td class="listmaintext">DAGR00401111111</td>
     </tr>
     <tr>
     <td class="listmaintext">ATM Centre:</td>
     <td class="listmaintext"></td>
     </tr>
     <tr>
     <td class="listmaintext">Site Location: </td>
     <td class="listmaintext">ADA Building - Agra</td>
     </tr>
</html>'''

soup = bs(html, 'lxml')
data = [' '.join((item.text, item.next_sibling.next_sibling.text)) for item in soup.select('#ContentPlaceHolder1_dlDetails tr .listmaintext:first-child') if item.text !='']
print(data)