J汤html解析样式=“;显示:无;不要显示链接
Jsoup html parsing style="display:none; dont show the link
我正在尝试在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>');
}
});
- Jquery在初始页面加载时选中复选框时显示链接的代码
- 当主导航链接悬停时,在侧菜单中显示链接
- 悬停时无法单击锚标记,但其显示链接
- 如何通过mailto在电子邮件中突出显示链接
- 如何在第一次点击时突出显示链接;并在第二次单击时跟随它,除非在其他地方单击,然后重新初始化未高亮显示的状态
- 希望仅为已登录的用户显示链接,否则它显示该链接在AngularJS和NodeJS中被隐藏或禁用
- 点击提交按钮后如何显示链接
- 如何使用 JavaScript 在共享按钮中显示链接
- 如何制作一个按钮,当悬停在上面时,会滑动并显示链接 (JS)
- 有条件地以角度显示链接.js
- JavaScript 函数不显示链接地址
- 显示链接的内容
- 在UI中显示链接函数angularJs的数据
- J汤html解析样式=“;显示:无;不要显示链接
- 将逻辑与幻灯片和隐藏/显示链接起来
- 可以在我的网站的另一个网站的地址栏中显示链接吗
- 如果低于特定的浏览器宽度,请不要显示链接
- 使用javascript在if条件内显示链接
- 如何在文本框中显示链接,并让按钮将其保存到数据库中
- 当用户单击“显示链接”时,显示密码,再次单击时将其隐藏