gpt4 book ai didi

python - 忽略 td beautifulsoup 中的 N/A 值

转载 作者:行者123 更新时间:2023-12-01 09:28:28 25 4
gpt4 key购买 nike

我想删除同类 td 中的 N/A 值

  <td align="left" class="category"> N/A</td>
<td align="left" class="title"> <a href="article-feb-0243.html">Wall Street cool to eBay's profit</a></td>
<td align="left" class="category"> technology</td>
<td align="left" class="title"> <a href="article-feb-2017.html">Warnings about junk mail deluge</a></td>
<td align="left" class="category"> technology</td>
<td align="left" class="title"> <a href="article-feb-2660.html">Web radio takes Spanish rap global</a></td>
<td align="left" class="category"> sport</td>

我想同时抓取类别和标题,但在类别中希望忽略 N/A 值

for td in parsed_html.body.findAll('td',{"class":lambda class_: class_ in ("category","title")}):
print(td)
category=td.parent.find("td",attrs={"class":"category"}).text

if(not td.parent.find("i")):
url=td.parent.find("a")["href"]

我尝试过将字符串与 N/A 匹配,但它不起作用

最佳答案

首先,您不必使用自定义函数来匹配多个类。您可以将不同的类作为列表传递。

其次,您可以通过两种方式获得您想要的东西。您可以简单地检查文本是否包含 N/A同时迭代所有 <td>标签并跳过该标签(如果存在)。

html = '''
<td align="left" class="category"> N/A</td>
<td align="left" class="title"> <a href="article-feb-0243.html">Wall Street cool to eBay's profit</a></td>
<td align="left" class="category"> technology</td>
<td align="left" class="title"> <a href="article-feb-2017.html">Warnings about junk mail deluge</a></td>
<td align="left" class="category"> technology</td>
<td align="left" class="title"> <a href="article-feb-2660.html">Web radio takes Spanish rap global</a></td>
<td align="left" class="category"> sport</td>'''

soup = BeautifulSoup(html, 'lxml')
for td in soup.find_all('td', class_=['category', 'title']):
if 'N/A' in td.text:
continue
print(td)

输出:

<td align="left" class="title"> <a href="article-feb-0243.html">Wall Street cool to eBay's profit</a></td>
<td align="left" class="category"> technology</td>
<td align="left" class="title"> <a href="article-feb-2017.html">Warnings about junk mail deluge</a></td>
<td align="left" class="category"> technology</td>
<td align="left" class="title"> <a href="article-feb-2660.html">Web radio takes Spanish rap global</a></td>
<td align="left" class="category"> sport</td>
<小时/>

您还可以使用自定义函数来执行此操作。

for td in soup.find_all(lambda tag: tag.name == 'td' and tag['class'][0] in ('category', 'title') and 'N/A' not in tag.text):
print(td)

关于python - 忽略 td beautifulsoup 中的 N/A 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50150626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com