作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想删除同类 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/
我是一名优秀的程序员,十分优秀!