繁体   English   中英

如何使用JQuery从html提取数据

[英]How to extract data from html using JQuery

我在下面有以下HTML代码(不是完整的html文件,但足以理解问题)。 我试图从h3元素以及p元素中的数据中提取值。 例如,我想使用这种格式:

NYC Row Restaurant Week
From April 20st through April 28th

Mother's Day at Restaurant
May 11th, 2014

A new lunch experience at Restaurant
Beginning February 5th

我之所以要提取这些值,是因为我想将它们输入到代码的其他部分(例如,jQuery手风琴类型的按钮)

下面的示例HTML

    <ul class="press">
                            <li>
                    <h3>
                        <a href="/about/calendar/nyc-row-restaurant-week-2">NYC Row Restaurant Week</a>                     </h3>
                    <p class="date">
                        From April 20st through April 28th                      </p>
                </li>
                                    <li>
                    <h3>
                        <a href="/about/calendar/mothers-day-at-restaurant-nyc">Mother&#039;s Day at Restaurant</a>                     </h3>
                    <p class="date">
                        May 11th, 2014                      </p>
                </li>
                                    <li>
                    <h3>
                        <a href="/about/calendar/a-new-lunch-experience-at-restaurant-4">A new lunch experience at Restaurant!</a>                      </h3>
                    <p class="date">
                        Beginning February 5th                      </p>
                </li>


                            <li>

我的JavaScript和HTML文件

<html>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"    
type="text/javascript"></script>
<script type="text/javascript">
var your_url = 'http://www.jaleo.com/about/calendar';
</script>

<script type="text/javascript">
// jquery.xdomainajax.js  ------ from padolsey

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'
        };

        // Since it's a JSONP request
        // complete === success
        if (!o.success && o.complete) {
            o.success = o.complete;
            delete o.complete;
        }

        o.success = (function(_success){
            return function(data) {

                if (_success) {
                    // Fake XHR callback.
                    _success.call(this, {
                        responseText: data.results[0]
                            // YQL screws with <script>s
                            // Get rid of them
                            .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
                    }, 'success');
                }

            };
        })(o.success);

    }

    return _ajax.apply(this, arguments);

};

})(jQuery.ajax);



 $.ajax({
   url: your_url,
   type: 'GET',
   success: function(res) {

   var result = res.responseText;
   var myHtml = $(result).find('h3').text();
   //I get stuck here and I'm not sure how to get the results in the format i specified above (in the question portion)
   alert(myHtml);

}
});
</script>
</html>

尝试提取子元素的文本,然后根据需要重新组合它们:

var arr = [];
$(result).find('li').each(function() {
    var title = $(this).find('h3 a').text();
    var dat = $(this).find('.date').text();
    arr.push(title + "<br>" + dat);
}); // arr is an array of HTML strings

使用each()遍历列表:

$.ajax({
   url: your_url,
   type: 'GET',
   success: function(res) {

   var result = res.responseText;
   var myHtml = $(result).find('li');

    myHtml.each(function(){
        alert($(this).find("p").text());
        alert($(this).find("h3").text());
    });

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM