【问题标题】:Pull Html element from cross domain从跨域拉取 Html 元素
【发布时间】:2023-04-08 16:41:01
【问题描述】:

我想通过 YQL 提取名为 class="row device-margin-b50" 的某个 div,但如果我输入 xpath="*",它不会提取确切的数据,它会带来整个页面,但我最后需要某些 div 我缺少什么?

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

【问题讨论】:

    标签: jquery html asp.net ajax yql


    【解决方案1】:

    尝试使用:

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

    这样你应该只得到你想要的类的div。

    【讨论】:

    • 您可能需要使用反斜杠转义引号,如下所示:xpath=\'//div[contains(@class,\"row device-margin-b50\")]\'跨度>
    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 2018-02-21
    • 2013-06-17
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多