从网站获取特定文本 (html)

Get specific text from a website (html)

本文关键字:html 文本 网站 获取      更新时间:2023-09-26

我想做一个小程序,这样我就可以快速轻松地收听这个广播电台。

http://www.offradio.gr/player

问题是我想不出任何方法来获取现在播放的曲目名称,制作人的名称和播放历史记录。

我想过从网站的原始源代码中提取特定数据,但源代码就像 4,000 行代码 - 对我来说太多了。

有什么想法吗?

我正在使用Visual Studio和C#

我知道这不是

最好的方法,但它是一个起点,它有效:

public Form1()
{
    InitializeComponent();
    webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted; // Subscribe event
    webBrowser1.Navigate("http://www.offradio.gr/player"); // Navigate to radio stream
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    /*
    Look for the element containing the element with the track number
    I've chosen this one because it has an ID means it's always the same div
    */
    HtmlElement parent = webBrowser1.Document.GetElementById("show_info");
    if (parent != null) // This event fires multiple times. Sometimes this element hasn't been created yet
    {
        /*
        We know it's a childless node inside `#show_info`.
        So let's just search for it.
        */
        foreach (HtmlElement child in parent.GetElementsByTagName("span"))
        {
                if (child.Children.Count == 0) // Check if it has children
                {
                    string title = child.InnerText; // The result
                    break;
                }
            }
        }
    }

不幸的是,我不得不使用.NET函数,使用JS,我会采取更简单的方法:

document.querySelector('#show_info .field-content').innerText

更新:

让我再给你一个提示。

查看document.getElementById('show_info').innerText的输出。

你可以解析它,你就完成了!

希望对你有帮助