在BeautifulSoup中查找没有属性的同级标签

时间:2019-07-12 00:35:32

标签: python python-3.x beautifulsoup

对不起,这是一个关于BeatifulSoup的初学者问题,但我找不到答案。

我在弄清楚如何抓取没有属性的HTML标签时遇到了麻烦。

这是代码部分。

<tr bgcolor="#ffffff">
 <td>
  No-Lobbying List
 </td>
 <tr bgcolor="#efefef">
  <td rowspan="2" valign="top">
   6/24/2019
  </td>
  <td>
   <a href="document.cfm?id=322577" target="_blank">
    Brian Manley, Chief of Police, Austin Police Department
   </a>
   <a href="document.cfm?id=322577" target="_blank">
    <img alt="Click here to download the PDF document" border="0"     height="16"     src="https://assets.austintexas.gov/edims/images/pdf_icon.gif"     width="16"/>
   </a>
  </td>
  <tr bgcolor="#efefef">
   <td>
    Preliminary 2018 Annual Crime Report - Executive Summary
   </td>
  </tr>
 </tr>
</tr>

我如何导航至带有文本“ 2018年度犯罪初步报告-执行摘要”的标签?

我尝试从具有属性的a移到.next_sibling,但是我失败了。

谢谢。

trgrewy = soup.findAll('tr', {'bgcolor':'#efefef'}) #the cells alternate colors
trwhite = soup.findAll('tr', {'bgcolor':'#ffffff'}) 
trs = trgrewy + trwhite #merge them into a list
for item in trs:
    mdate = item.find('td', {'rowspan':'2'}) #find if it's today's date
    if mdate:
        datetime_object = datetime.strptime(mdate.text, '%m/%d/%Y')
        if datetime_object.date() == now.date():
            sender = item.find('a').text
            pdf = item.find('a')['href']
            link = baseurl + pdf
            title = item.findAll('td')[2] #this is where i've failed

2 个答案:

答案 0 :(得分:1)

您可以使用CSS选择器:

data = '''
<tr bgcolor="#ffffff">
 <td>
  No-Lobbying List
 </td>
 <tr bgcolor="#efefef">
  <td rowspan="2" valign="top">
   6/24/2019
  </td>
  <td>
   <a href="document.cfm?id=322577" target="_blank">
    Brian Manley, Chief of Police, Austin Police Department
   </a>
   <a href="document.cfm?id=322577" target="_blank">
    <img alt="Click here to download the PDF document" border="0"     height="16"     src="https://assets.austintexas.gov/edims/images/pdf_icon.gif"     width="16"/>
   </a>
  </td>
  <tr bgcolor="#efefef">
   <td>
    Preliminary 2018 Annual Crime Report - Executive Summary
   </td>
  </tr>
 </tr>
</tr>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'lxml')

# This will find date
print(soup.select_one('td[rowspan="2"]').get_text(strip=True))

# This will find next row after the row with date
print(soup.select_one('tr:has(td[rowspan="2"]) + tr').get_text(strip=True))

打印:

6/24/2019
Preliminary 2018 Annual Crime Report - Executive Summary

进一步阅读:

CSS Selectors Reference

答案 1 :(得分:0)

我认为您应该尝试

page = BeautifulSoup(HTML_TEXT)

text = page.find('td').findAll(text=True, recursive=False)

for i in text:
    print i
相关问题