我正在尝试在 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/
问候。 欧麦
最佳答案
当您在浏览器中检查元素时,您会在 javascript 运行后获得 DOM。 Jsoup 是一个解析器,因此您可以在任何 javascript 运行之前获取 html。如果您想检查 Jsoup 将获取的实际 html,请使用 chrome 按 Ctrl+U(我认为这与 mozilla 相同)。这是您从服务器获得的 html 响应,没有任何 javascript 修改内容。
考虑到以上内容,href
属性正在被一些 javascript 代码修改。这不能由 Jsoup 处理,因为它不能执行 javascript。您必须找出修改 href
属性的 javascript 代码,并使用 java 的 ScriptEngine 执行它。或者使用可以处理 javascript 执行的解析器,如 Selenium .
如果您分享您尝试解析的页面的链接,我可以提供更多帮助。
更新
有了它,您将不必再使用 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 有很多依赖关系,这使得它运行起来很麻烦 在安卓系统上。可以找到更好的解决方案here和 here
更新
$(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>');
}
});
关于javascript - jsoup html解析style=”display :none; dont show the link,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26700152/