Jsoup html解析风格="显示:无;不要显示链接

时间:2014-11-02 14:03:25

标签: javascript android html css jsoup

我正在尝试用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/

的问候。 奥马尔

1 个答案:

答案 0 :(得分:2)

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

考虑到上述情况,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"));
    }
}

<强>更新

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

<强>更新

$(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>');
    }
});