How to print only a specific link in Python

Viewed 916

I'm still a newbie in Python but I'm trying to make my first little program. My intention is to print only the link ending with .m3u8 (if available) istead of printing the whole web page. The code I'm currently using:

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...')

The link I'm looking for always has 47 characters in total, and it's always the same model just changing the stream id represented as X:

https://website.tv/live/streamidXXXXXXXXX.m3u8

Can anyone help me?

5 Answers

You can use regex for this problem.

Explanation:

here in the expression portion .*? means to consider everything and whatever enclosed in \b(expr)\b needs to be present there mandatorily.

For e.g.:

import re

link="https://website.tv/live/streamidXXXXXXXXX.m3u8"

p=re.findall(r'.*?\b.m3u8\b',link)
print(p)

OUTPUT:

['https://website.tv/live/streamidXXXXXXXXX.m3u8']

There are a few ways to go about this, one that springs to mind which others have touched upon is using regex with findall that returns back a list of matched urls from our url_list.

Another option could also be BeautifulSoup but without more information regarding the html structure it may not be the best tool here.

Using Regex

from re import findall
from requests import get


def check_link(response):
    result = findall(
        r'.*?\b.m3u8\b',
        str(response.content),
    )
    return result

def main(url):
    response = get(url)
    if response.ok:
        link_found = check_link(response)
        if link_found:
            print('link {} found at {}'.format(
                    link_found,
                    url,
                ),
            )

if __name__ == '__main__':
    url_list = [
        'http://www.test_1.com',
        'http://www.test_2.com',
        'http://www.test_3.com',
    ]
    for url in url_list:
        main(url)

    print("All finished")

This will extract all URLs from webpage and filter only those which contain your required keyword ".m3u8"

import requests
import re
def get_desired_url(data):
    urls = []
    for url in re.findall(r'(https?://\S+)', data):
        if ".m3u8" in url:
            urls.append(url)
    return urls

channel1 = requests.get('https://website.tv/user/111111')
urls = get_desired_url(channel1 )

If I understand your question correctly I think you want to use Python's .split() string method. If your goal is to take a string like "https://website.tv/live/streamidXXXXXXXXX.m3u8" and extract just "streamidXXXXXXXXX.m3u8" then you could do that with the following code:

web_address = "https://website.tv/live/streamidXXXXXXXXX.m3u8"
specific_file = web_address.split('/')[-1]
print(specific_file)

The calling .split('/') on the string like that will return a list of strings where each item in the list is a different part of the string (first part being "https:", etc.). The last one of these (index [-1]) will be the file extension you want.

Try this, I think this will be robust

import re

links=[re.sub('^<[ ]*a[ ]+.*href[ ]*=[ ]*',  '', re.sub('.*>$', '', link) for link in re.findall(r'<[ ]*a[ ]+.*href[ ]*=[]*"http[s]*://.+\.m3u8".*>',channel2.content)]
Related