【发布时间】:2020-06-28 16:38:48
【问题描述】:
想象一下,您正尝试使用 bs4 解析类似的内容:
<table>
<tbody>
<tr>
<th attr="attr" class="title">
<a href="link.com/arhwth">Title Text</a>
</th>
<th attr="attr" class="title">
<a href="link.com/dfdsth">Title Text 2</a>
</th>
<th attr="attr" class="title">
<a href="link.com/gsfbf">Title Text 3</a>
</th>
</tr>
</tbody>
<a href"otherlink.com">Other link to throw you off</a>
</table>
目前我可以通过
获得所有th 元素的列表
html_content = BeautifulSoup(requests.get("parsingwebsite.com").content, "html.parser")
res = html_content.find_all("th", {"attr": "attr"}, class_="title")
但我只想要<a> 中的标题文本。 (最好是["Title Text", "Title Text 2", "Title Text 3"])
有没有办法继续通过 html 元素向下过滤或以其他方式修改原始查询以过滤到链接内的文本,而不必使用正则表达式?
【问题讨论】:
标签: python html python-3.x beautifulsoup