如何使用jQuery阅读网页并提取某些链接
How to Read a webpage and extract certain links using jQuery?
在同一个域名上。我的Jquery代码和url读取。
我想做的是首先使用Jquery读取网页,然后解析某些链接,其中有"ProductDetails.php"和提取"ProductCode"从网页到数组。
html页面可能有许多href="ProductDetails.php实例,如下所示。
<a href="ProductDetails.php?ProductCode=SMS%2D15%2DXLG%2DA7&CartID=1" class="carttext colors_productname cart-item-name">item 1 <a>
<a href="ProductDetails.php?ProductCode=SMS%dfdfde&CartID=2" class="carttext colors_productname cart-item-name">test me item <a>
我不知道这是否真的可能
你必须这样做:
var filteredAnchors = $( document.body ).find( 'a' ).map(function( _, anchor ) {
if( anchor.getAttribute('href').indexOf( 'ProductDetails.php' ) === 0 ) {
return anchor.getAttribute('href').match( /ProductCode=(.*?)&/ )[ 1 ];
}
}).get();
filteredAnchors
现在应该包含所有的产品代码。
示例:http://jsfiddle.net/WgwSr/
像这样的东西应该让你开始:
$.ajax({
url: "pagetoload.html",
success: function(htmlofthepage) {
var html = $(htmlofthepage),
resultarray = []; // the array containing our final result set
// getting all of the anchor tags we want to look at
$('a[href^="ProductDetails.php"]', html).each(function () {
var t = $(this), // the anchor tag
href = t.prop('href'), // the href of the tag (eg. ProductDetails.php?...)
start = href.indexOf('ProductCode', 0),
begin = 0,
end = 0;
if (start > -1) {
begin = href.indexOf('=', start) + 1;
end = href.indexOf('&', begin);
resultarray.push(href.split(begin, end));
}
});
}
});
使用jQuerys的each
函数:
jQuery(function($){
var links = [];
$("a[href^=ProductDetails.php]").each(function(){
links.push(this.href.replace(/^.*'?/,'');
});
});
相关文章:
- 给定带有位置“loc”的链接,搜索使用谷歌地图API从标记中提取纬度和经度
- 如何在用户提交链接时添加提取推文并将其发布到站点的功能
- JavaScript链接提取器
- 从html文件中的js中提取链接
- Tinymce-4:仅从链接中提取文本
- 如何在 Java 中从 HTML 页面中提取 JavaScript 链接
- 同时提取两个链接
- 提取 URL 的中间部分并将其附加到新的外部链接以在 IE8 中显示
- xPath - 从锚链接中提取 href 的内容
- 提取 SRC 并使用 jQuery 粘贴之前在 HTML 链接中
- 提取html页面中所有以.js扩展名结尾的链接
- 从Javascript关联数组中提取HTML链接以应用于D3生成的表
- JavaScript如何从所选文本中提取链接
- 使用Javascript提取外部动态链接
- 如何从链接中提取href属性并创建该属性的特定模式
- Node.js Bot从消息中提取链接
- 使用JavaScript书签从图像链接中提取alt文本
- 从Javascript/Regex中的youtube链接中提取视频ID
- 如何使用jQuery阅读网页并提取某些链接
- 如何使用javascript从iframe中提取链接