我需要使用inspect元素从网站获取特定数据

2024-05-12 13:05:33 发布

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

我是Python新手,正在努力寻找解决方案或方法来完成以下工作。我需要从一个网站上获得两样东西,我可以从inspect元素中获得:指向.m3u8文件的链接,该文件可以在网站的html(元素选项卡)中找到,以及指向网络选项卡中的.ts文件的链接(不管是哪一个)。有人知道怎么做吗?提前谢谢


Tags: 文件方法网络元素网站链接html解决方案
2条回答

使用BS4和请求:

import requests
from bs4 import BeautifulSoup

URL = 'https://stackoverflow.com/questions/64828046/'
page = requests.get(URL)

soup = BeautifulSoup(page.content, 'html.parser')

results = soup.find(id='question-header')
print(results)
from urllib.request import urlopen
import lxml.html
connection = urlopen('http://yourwebsite')

dom = lxml.html.fromstring(connection.read())
for link in dom.xpath('//a/@href'):
    if link.endswith(".m3u8") or link.endwith(".ts"):
        print(link)

您可以使用其他if conditions检查链接中是否有内容,如:

if "m3u8" in link:
   print(link)

相关问题 更多 >