Pull Html element from cross domain

我的梦境 提交于 2019-12-13 05:07:30

问题


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>

回答1:


Try using:

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

This way you should only get div with class you wanted.



来源:https://stackoverflow.com/questions/32587995/pull-html-element-from-cross-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!