查找以.rss结尾的带有python beautifulsoup4的url

2024-07-04 05:26:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图找到一种方法来获得类似于itunes电影预告片的rss提要url

<a href="http://trailers.apple.com/trailers/home/rss/newtrailers.rss">

如何使用beautifulsoup匹配以.rss结尾的URL?你知道吗


Tags: 方法comhttpurlapplehome电影itunes
2条回答

您可以使用re模块并传递正则表达式模式以匹配属性,例如,要匹配字符串末尾的rss,可以使用rss$

soup = BeautifulSoup("""<a href="http://trailers.apple.com/trailers/home/rss/newtrailers.rss"></a>
<a href="http://trailers.apple.com/trailers/home/rss/newtrailers"></a>""", "html.parser")

import re
soup.find_all("a", {"href": re.compile("rss$")})
# [<a href="http://trailers.apple.com/trailers/home/rss/newtrailers.rss"></a>]

您可以遍历页面中找到的所有a标记,并检查它们的href字段是否以.rss结尾

for link in page.findAll(`a`):
    if link['href'].endswith('.rss'):
        **do something**

相关问题 更多 >

    热门问题