J汤html解析样式=“;显示:无;不要显示链接

Jsoup html parsing style="display:none; dont show the link

本文关键字:显示 链接 html 样式      更新时间:2023-09-26

我正在尝试在android中使用Jsoup解析数据。但出了问题,我不是一个有html css天赋的人,所以我无法解决它。我正在尝试解析html数据。但它在页面来源和我在mozilla中通过"检查项目"获得的数据方面有所不同。

当我用mozilla检查项目时,我得到:

<a id="download_link" href="http://dl4.downloader.info/dym_down.php?id=f402e15c02f64b02da8e45535e95600a" target="_blank" rel="nofollow" style="">Download</a>

但在页面的来源中,数据是

<a id="download_link" href="[FILE_LINK]" target="_blank" rel="nofollow" style="display:none;">Download</a> <a id="go_back" href="" rel="nofollow">Go back</a>

当我尝试在android中使用jsoup获取这些数据时,我得到了第二个数据。也许问题是因为style="display:none;"

如果是因为style="display:none;",我怎么能把数据作为"http://dl4.downloader.info/dym_down.php?id=f402e15c02f64b02da8e45535e95600a"而不是[FILE_LINK]。编辑:链接:http://www.mp3juices.cc/download/sEhy-RXkNo0/mp3/rihanna_-_man_down/

谨致问候。Omer

当您在浏览器中检查元素时,您会在javascript运行后得到DOM。Jsoup是一个解析器,因此您可以在运行任何javascript之前获得html。如果你想检查Jsoup将获取的实际html,请按Ctrl+U并使用chrome(我认为这与mozilla相同)。这是在没有任何javascript修改内容的情况下从服务器获得的html响应。

考虑到以上内容,href属性正在被一些javascript代码修改。Jsoup无法处理此问题,因为它无法执行javascript。您必须找到修改href属性的javascript代码,并使用java的ScriptEngine或可以处理诸如Selenium之类的javascript执行的解析器来执行它。

如果你分享你试图解析的页面的链接,我可以提供更多帮助。

更新

有了这个,你就不用再使用Jsoup了。如果HtmlUnit不适合您作为解析器的需要只需使用它来获得完整的html(在执行js之后),然后用Jsoup解析它并执行从那里。你必须下载HtmlUnit。在类路径中包含jar,或者使用maven。

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class Main {
    public static void main(String[] args) throws Exception {
        final WebClient webClient = new WebClient();
        final HtmlPage page = webClient.getPage("http://www.mp3juices.cc/download/sEhy-RXkNo0/mp3/rihanna_-_man_down/");
        webClient.closeAllWindows();
        System.out.println(page.getElementById("download_link", false).getAttribute("href"));
    }
}

更新

根据这一点,HtmlUnit有许多依赖项,这使得它运行起来非常麻烦在Android上。更好的解决方案可以在这里和这里找到

更新

$(document).ready(function() {  //jQuery : Begin executing the js code when the page is fully loaded
    var $container = $('#vid');  //jQuery : Get the messages container. Irrelevant
    var Complete = false;        //A boolean
    var URL = 'http://www.mp3juices.cc/download/sEhy-RXkNo0/mp3/rihanna_-_man_down/'.split('/'); //The url that indicates the file
    var Video = '';
    function Convert(Hash) {
        $.ajax({
            url: 'http://data.downloader.info/dym_state.php', //Internal web service of the site,  from where it get the data (download link)
            data: {
                id: Hash            //The parameters added in the url (it makes an HTTP GET request, so the url becomes like this http://data.downloader.info/dym_state.php?id=Hash where Hash is a value given as a parameter in Convert
            },
            dataType: 'jsonp',      //Google this. It's a little different from json. There are hacks for java
            success: function(Data) {   //If the request succeeds, the data brough back are in the variable Data
                Data = Data.state.split(' - ');     //Obvious
                $.each(Data, function(Index, Value) {   //iteration. Check the data that the request returns and you will understand
                    Data[Index] = parseInt(Value);  //Value variable indicates the state. 
                });
                switch (Data[0]) {
                    case 1:
                    case 2:
                        $container.append('Converting video ...<br>');  //if Value == 1 or Value == 2 
                        break;
                    case 3:
                        Complete = true;
                        $container.append('The file is ready. Please click the download button to start the download.<br>');
                        $container.append('http://dl' + parseInt((Data[1] + 1)) + '.downloader.info/dym_down.php?id=' + Hash);  //This is the url that you want
                        break;
                    case 5:
                        Complete = true;
                        $container.append('An error has occured. Please try to download a different song.<br>');
                        break;
                }
                if (!Complete) {
                    window.setTimeout(function() {  //Here it makes again and again the same request until it gets Value == 3 or if an error occured 
                        Convert(Hash);
                    }, 3000);   //Every 3000 millisecs (3 seconds)
                }
            }
        });
    }
    //This is executed first, and calls the Convert function from above. This here computes the hash that you have to pass as a parameter in Convert
    if (6 < URL.length && (Video = new RegExp('[a-zA-Z0-9'-'_]{11}').exec(URL[4]))) {   //Straightforward 
        Video = Video.toString();
        if (URL[5] != 'mp3' && URL[5] != 'mp4') {
            $container.append('Please enter a valid format.<br>');
            return false;
        }
        $.ajax({    //Makes a GET request
            url: 'http://data.downloader.info/ytd.php', //The url that it makes the request to.
            data: {         //The data. The url becomes like this 'http://data.downloader.info/ytd.php?v=URL[4]&f=URL[5]&s=n/a&e=n/a&sig=1337'
                v: URL[4],
                f: URL[5],
                s: 'n/a',
                e: 'n/a',
                sig: 1337
            },
            dataType: 'jsonp',
            success: function(Data) {
                if (-1 < Data.error.indexOf(5)) {
                    $container.append('An error has occured. Please try to convert a different video.<br>');
                    return false;
                }
                $container.append(Data.title);
                Convert(Data.hash);
            }
        });
    } else {
        $container.append('Please enter a valid YouTube Video ID.<br>');
    }
});