如果单元格满足特定条件,则从行中提取特定值

时间:2019-07-16 19:28:59

标签: python beautifulsoup

我有这个HTML文件,该文件是从具有财务数据的网站获得的。

    <table class="tableFile2" summary="Results">
     <tr>
      <td nowrap="nowrap">
       13F-HR
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-05-15
      </td>
      <td nowrap="nowrap">
       <a href="URL">
        028-10098
       </a>
       <br/>
       19827821
      </td>
     </tr>
     <tr class="blueRow">
      <td nowrap="nowrap">
       13F-HR
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-14
      </td>
      <td nowrap="nowrap">
       <a href="URL">
        028-10098
       </a>
       <br/>
       19606811
      </td>
     </tr>
     <tr>
      <td nowrap="nowrap">
       SC 13G/A
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-13
      </td>
      <td>
      </td>
     </tr>
     <tr class="blueRow">
      <td nowrap="nowrap">
       SC 13G/A
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-13
      </td>
      <td>
      </td>
     </tr>
     <tr>
      <td nowrap="nowrap">
       SC 13G/A
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-13
      </td>
      <td>
      </td>
     </tr>
    </table>

我尝试仅提取其中一个单元格包含单词 13F 的行。获得正确的行后,我希望能够将日期和href保存到列表中以供以后处理。目前,我成功构建了我的抓取工具以成功找到特定的表,但是我无法根据自己的条件过滤特定的行。目前,当我尝试添加条件时,似乎会忽略它,并继续在所有行中都包含行。

r = requests.get(url)
soup = BeautifulSoup(open("data/testHTML.html"), 'html.parser')

table = soup.find('table', {"class": "tableFile2"})
rows = table.findChildren("tr")
for row in rows:
    cell = row.findNext("td")
    if cell.text.find('13F'):
        print(row)

理想情况下,我正在尝试获得与此类似的输出

[13F-HR,URL,2019-05-15],[13F-HR,URL,2019-02-14]

2 个答案:

答案 0 :(得分:1)

使用regular表达式re查找单元格的文本。

from bs4 import BeautifulSoup
import re
data='''<table class="tableFile2" summary="Results">
     <tr>
      <td nowrap="nowrap">
       13F-HR
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-05-15
      </td>
      <td nowrap="nowrap">
       <a href="URL">
        028-10098
       </a>
       <br/>
       19827821
      </td>
     </tr>
     <tr class="blueRow">
      <td nowrap="nowrap">
       13F-HR
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-14
      </td>
      <td nowrap="nowrap">
       <a href="URL">
        028-10098
       </a>
       <br/>
       19606811
      </td>
     </tr>
     <tr>
      <td nowrap="nowrap">
       SC 13G/A
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-13
      </td>
      <td>
      </td>
     </tr>
     <tr class="blueRow">
      <td nowrap="nowrap">
       SC 13G/A
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-13
      </td>
      <td>
      </td>
     </tr>
     <tr>
      <td nowrap="nowrap">
       SC 13G/A
      </td>
      <td nowrap="nowrap">
       <a href="URL" id="documentsbutton">
        Documents
       </a>
      </td>
      <td>
       2019-02-13
      </td>
      <td>
      </td>
     </tr>
    </table>'''

soup=BeautifulSoup(data,'html.parser')
table = soup.find('table', {"class": "tableFile2"})
rows=table.find_all('tr')

final_items=[]
for row in rows:
    items = []
    cell=row.find('td',text=re.compile('13F'))
    if cell:
        items.append(cell.text.strip())
        items.append(cell.find_next('a')['href'])
        items.append(cell.find_next('a').find_next('td').text.strip())
        final_items.append(items)

print(final_items)

输出:

 [['13F-HR', 'URL', '2019-05-15'], ['13F-HR', 'URL', '2019-02-14']]

答案 1 :(得分:1)

优化的解决方案:

...

for tr in soup.select('table.tableFile2 tr'):
    tds = tr.findChildren('td')
    if '13F' in tds[0].text:
        print([td.text.strip() for td in tds[:3]])

输出:

['13F-HR', 'Documents', '2019-05-15']
['13F-HR', 'Documents', '2019-02-14']