我的汤数据如下
<a href="/title/tt0110912/" title="Quentin Tarantino">
Pulp Fiction
</a>
<a href="/title/tt0137523/" title="David Fincher">
Fight Club
</a>
<a href="blablabla" title="Yet to Release">
Yet to Release
</a>
<a href="something" title="Movies">
Coming soon
</a>
我需要来自那些a
标记的文本数据,条件可能是href=/title/*wildcharacter*
我的眼睛可能有点像这样
titles = []
for a in soup.find_all("a",href=True):
if a.text:
titles.append(a.text.replace('\n'," "))
print(titles)
但是在这个条件下,我从所有a
标记中获取文本。我只需要href
有"/title/***"
的文本
您可以使用正则表达式搜索属性的内容(在本例中为href)
有关更多详细信息,请参阅以下答案:https://stackoverflow.com/a/47091570/1426630
1.)要获取所有
<a>
标记,其中href=
以"/title/"
开头,可以使用CSS选择器a[href^="/title/"]
2.)要去除标记内的所有文本,可以使用
.get_text()
和参数strip=True
印刷品:
我猜你想要这样:
输出:
相关问题 更多 >
编程相关推荐