[英]Checking if URL ends in specific top-level domain .tv
I'm using praw the Reddit developer extension to take the title from a certain subreddit. 我正在使用Prad的Reddit开发人员扩展从某个subreddit获得标题。 I want to only take the title if the url the post directs to ends in .tv. 我只想在帖子所指向的URL以.tv结尾的情况下使用标题。
How to extract specific URLs that contain the top-level domain .tv and append them to their own list? 如何提取包含顶级域.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)
I will assume that the URL can be be http://abtv/etc or even http://abtv:80/etc , so: 我将假定该URL可以是http:// abtv / etc甚至是http:// abtv: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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.