本文介绍了从拉跨域HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想拉某的div名为类=行设备毛利率-B50
通过YQL,但它不拉的确切数据,如果我把的XPath =*
,它带来的整个页面,但我需要一定的格在我结束请告诉我,我缺少什么呢?
<头=服务器>
<脚本SRC =jQuery的-2.1.4.js>< / SCRIPT>
< /头
<脚本>
jQuery.ajax =(函数(_ajax){
VAR协议= location.protocol,
主机名= location.hostname,
exRegex =正则表达式(协议+'//'+主机名),
YQL =HTTP+(/^https/.test(protocol)的':'')+'?:?//query.yahooapis.com/v1/public/yql回调=',
查询=SELECT * FROM HTML其中,url ={网址}和XPath =*';
功能isExternal(URL){
返回exRegex.test(URL)及!&安培; /:\/\//.test(url);
}
复位功能(O){
VAR URL = o.url;
如果(/get/i.test(o.type)及&安培;!/json/i.test(o.dataType)及&安培; isExternal(URL)){
//操纵选项,使JSONP-X请求发送到YQL
o.url = YQL;
o.dataType ='json的';
o.data = {
问:query.replace(
{网址},
URL +(o.data?
(/\?/.test(url)'&安培;':?)+ jQuery.param(o.data)'?'
:'')
),
格式:XML
};
如果(o.success&安培;!&安培; o.complete){
o.success = o.complete;
删除o.complete;
}
o.success =(函数(_success){
返回功能(数据){
如果(_success){
_success.call(这一点,{
responseText的:(data.results [0] ||'')
.replace(/&所述;脚本[^>] + \ /> |?&所述;?脚本(| \多个)* \ /脚本> / GI,'')
}, '成功');
}
};
})(o.success);
}
返回_ajax.apply(这一点,参数);
};
})(jQuery.ajax);
$阿贾克斯({
网址:http://www.mtbc.com/about-us/$p$pss-room/,
键入:GET,
成功:函数(RES){
执行console.log(RES);
$('#内容)HTML(res.responseText);
}
});
< / SCRIPT>
HTML标记
< DIV ID =内容>
搜索....
< / DIV>
解决方案
请尝试使用:
的XPath ='// DIV [含有(@class,排装置利润-B50)
这样,你应该只得到分度,你想要的类。
I want to pull the certain div named class="row device-margin-b50"
through YQL but its not pulling the exact data if I put xpath="*"
, it brings the whole page but I need certain div at my end Whats I'm lacking?
<head runat="server">
<script src="jquery-2.1.4.js"></script>
</head
<script>
jQuery.ajax = (function (_ajax) {
var protocol = location.protocol,
hostname = location.hostname,
exRegex = RegExp(protocol + '//' + hostname),
YQL = 'http' + (/^https/.test(protocol) ? 's' : '') + '://query.yahooapis.com/v1/public/yql?callback=?',
query = 'select * from html where url="{URL}" and xpath="*"';
function isExternal(url) {
return !exRegex.test(url) && /:\/\//.test(url);
}
return function (o) {
var url = o.url;
if (/get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url)) {
// Manipulate options so that JSONP-x request is made to YQL
o.url = YQL;
o.dataType = 'json';
o.data = {
q: query.replace(
'{URL}',
url + (o.data ?
(/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
: '')
),
format: 'xml'
};
if (!o.success && o.complete) {
o.success = o.complete;
delete o.complete;
}
o.success = (function (_success) {
return function (data) {
if (_success) {
_success.call(this, {
responseText: (data.results[0] || '')
.replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
}, 'success');
}
};
})(o.success);
}
return _ajax.apply(this, arguments);
};
})(jQuery.ajax);
$.ajax({
url: 'http://www.mtbc.com/about-us/press-room/',
type: 'GET',
success: function (res) {
console.log(res);
$('#content').html(res.responseText);
}
});
</script>
Html Markup
<div id="content">
Searching....
</div>
解决方案
Try using:
xpath='//div[contains(@class,"row device-margin-b50")]'
This way you should only get div with class you wanted.
这篇关于从拉跨域HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!