简体   繁体   中英

How to extract an value from an external html page in javascript/jquery

I have a link example this Now I want to get the source of this page and extract the md5 hash value which is something like

<strong>MD5:</strong> 1b061e5530d2612135b8896482e68e3c</div>
<div>

I want to get the value 1b061e5530d2612135b8896482e68e3c from it.

I have made an GET request and got the source code in an variable like:

$.get(link).done(function(data){
    alert(data);
});

This seems to be working fine but I have no Idea how to proceed further kindly help me . I have Searched but not got any helpful result.

You can use to get the text node containing the MD5 hash value:

$.get(link).done(function (data) {
    var md5Node = document.evaluate('//*[@id="app_info"]/div[1]/div[2]/div[2]/div[1]/text()', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    var md5String = md5Node.textContent;
    // ...
});

A better XPath

It would be better to find the MD5 text node based on its sibling's value. The XPath would look like

//*[text()="MD5:"]/../text()

You could use good, old fashioned (ie, the second result of /MD5:<\\/strong> (.*?)<\\/div>/g ).

var result = (/MD5:<\/strong> (.*?)<\/div>/g).exec([text])[1];

 var regex = /MD5:<\\/strong> (.*?)<\\/div>/g; var output=document.getElementById("output"); var test="<strong>MD5:</strong> 1b061e5530d2612135b8896482e68e3c</div>"; var matches = regex.exec(test); output.innerHTML="MD5 is "+matches[1]; 
 <div id="output"></div> 

Does the div you're trying to get the value from have an ID? If so you could try something like this

$.get(link).done(function(data){ console.log($(data).find("#idofdiv").text()); });

您还可以使用jQuery的.load函数,如下所示:

$('div#container').load('external-page.html div#md5');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM