【问题标题】:Checking if URL ends in specific top-level domain .tv检查 URL 是否以特定顶级域 .tv 结尾
【发布时间】:2019-08-16 10:07:57
【问题描述】:

我正在使用 praw Reddit 开发者扩展从某个 subreddit 中获取标题。如果帖子指向的 url 以 .tv 结尾,我只想取标题。

如何提取包含顶级域 .tv 的特定 URL 并将它们附加到自己的列表中?

    import praw
    reddit = praw.Reddit(client_id='', client_secret='', user_agent='')
    hot_p = reddit.subreddit('music').top('week')

    for post in hot_p:
    # if post.url ends in .tv... 
        raw_titles.append(post.title)
        raw_url.append(post.url)

【问题讨论】:

标签: python


【解决方案1】:

我假设 URL 可以是http://a.b.tv/etc 甚至是http://a.b.tv:80/etc,所以:

from urllib.parse import urlparse

for post in hot_p:
    o = urlparse(post.url)
    top_level_domain = o.netloc.split('.')[-1].split(':')[0]
    if top_level_domain == 'tv':
        raw_titles.append(post.title)
        raw_url.append(post.url)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多