从跨域拉取Html元素

时间:2015-09-15 13:55:42

标签: jquery html asp.net ajax yql

我想通过YQL拉出名为class="row device-margin-b50"的某个div但是如果我放xpath="*"它没有提取确切的数据,它会带来整个页面,但我需要一定的div在我的结尾是什么我&# 39; m缺乏?

 <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标记

<div id="content">
    Searching....
    </div>

1 个答案:

答案 0 :(得分:2)

尝试使用:

xpath='//div[contains(@class,"row device-margin-b50")]'

这样你应该只得到你想要的课程。