我仍然是Python的新手,但我正在尝试制作我的第一个小程序。 我的目的是只打印以.m3u8结尾的链接(如果可用),而不打印整个网页。 我当前使用的代码:
import requests
channel1 = requests.get('https://website.tv/user/111111')
print(channel1.content)
print('\n')
channel2 = requests.get('https://website.tv/user/222222')
print(channel2.content)
print('\n')
input('Press Enter to Exit...')
我要查找的链接始终总共有47个字符,并且始终是同一个模型,只需更改表示为X的流id:
https://website.tv/live/streamidXXXXXXXXX.m3u8
有人能帮我吗
如果我正确理解了您的问题,我想您应该使用Python的
.split()
字符串方法。如果您的目标是获取像"https://website.tv/live/streamidXXXXXXXXX.m3u8"
这样的字符串并仅提取"streamidXXXXXXXXX.m3u8"
,那么您可以使用以下代码来实现这一点:对这样的字符串调用
.split('/')
将返回一个字符串列表,其中列表中的每个项都是字符串的不同部分(第一部分是"https:"
,等等)。最后一个(索引[-1])将是您想要的文件扩展名有几种方法可以做到这一点,让人想到其他人已经提到过的一种方法是使用regex和
findall
一起返回我们的url_list
中匹配的URL列表另一个选项也可以是BeautifulSoup,但如果没有更多关于
html
结构的信息,它可能不是这里最好的工具使用Regex
您可以使用正则表达式解决此问题
说明:
这里是表达式部分。*?意味着要考虑所有的东西,以及在B(EXPR)\B中所包含的任何东西都需要强制地存在。p>
例如:
输出:
相关问题 更多 >
编程相关推荐