2

所以我一直在研究一种从页面中抓取数据并显示它的方法(与源的格式大致相同)。我找到了 YQL,我发现它很棒,除了我不知道如何只显示整个输出而没有什么特别的(除了基本格式)

YQL 输入代码为:

select * from html where url="http://directory.vancouver.wsu.edu/anthropology" and xpath="//div[@id='facdir']"

使用它返回 JSON:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdirectory.vancouver.wsu.edu%2Fanthropology%22%20and%20xpath%3D%22%2F%2Fdiv%5B%40id%3D'facdir'%5D%22&format=json&callback=anthropology

我遵循了 yahoo 教程,并创建了新闻小部件等,但没有一个教程涵盖基本视图(也不需要链接,只需要段落设置)。

像这样:

Name
Title
Phone:(###)###-####
Location: Building and Room #
email@vancouver.wsu.edu

这是我从http://christianheilmann.com获得的输出,但它什么也没做(显然她的教程都不起作用,每个教程都试过了):

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
</head>
<body>
<p>
<b>Copied:</b>
</p>
<div>
<script>
function anthropology (0) {
// get the DIV with the ID $
var info = document.getElementById('facdir');
// add a class for styling
info.className = 'js';
// if it exists
if(info){
// get the info data returned from YQL
var data = o.query.results.span;
var link = info.getElementsByTagName('a')[0];
link.innerHTML = '(see all info)';
// to the main container DIV
var out = document.createElement('span');
out.className = 'info';
info.insertBefore(out,link.parentNode);
}
}
</script>
<script src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdirectory.vancouver.wsu.edu%2Fanthropology%22%20and%20xpath%3D%22%2F%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%22&format=json&callback=anthropology'></script>
</div>
4

1 回答 1

4

我最近完成了一个包含几个 jsFiddles 的教程,并解释了如何使用YQLXPATH和 jQuery.ajax()来解决不同的 SO 问题,这将为您的方向提供一些启示。你可以在这里看到SO Answer

为了满足您问题的可接受答案,我整理了一个工作演示,向您展示从您请求的网页中抓取数据是多么容易。

jsFiddle Demo 包含大量评论和console.log()消息,以了解工作流程。确保您激活浏览器控制台并使用Firebug。用于构建教职员工框HTML和模仿原始网站中的那些框,包括图像、名称、电子邮件和网页主题中的链接。CSS

演示:

jsFiddle 数据抓取 XML:动态网页构建

修改!!! 除了上面修改的jsFiddle,见相关

jsFiddle 教程:创建动态 Div(现已改进!

HTML:

<div id="results"></div>

jQuery:

var directoryName = 'child-development-program';

$.ajax({
    type: 'GET',
    url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdirectory.vancouver.wsu.edu%2F" + directoryName + "%22%20and%20xpath%3D%22%2F%2Fdiv%5B%40id%3D'content-inner'%5D%2Fdiv%2Fdiv%2Fdiv%2Fdiv%2Fdiv%5B2%5D%22",
    dataType: 'xml',
    success: function(data) {

        if (data) {

            // Show in console the jQuery Object.
            console.info('Here is the returned query');
            console.log( $(data).find('query') );

            // Show in console the results in inner-html text.
            var textResults = $(data).find('results').text();
            console.log( textResults );

            // Parse the list of faculty members. Variable indexFM is not used for indexed faculty member.
            $(data).find('results').find('.views-row').each(function(indexFM){

                // This variable will store the current faculty member.
                var facultyMember = this;
                console.info('Faculty jQuery DIV Object shown on next lines.');
                console.log( facultyMember );

                // Parse the contents of each faculty member. Variable indexFC is not used for indexed faculty content.
                $(facultyMember).each(function(indexFC){

                    // Get Thumbnail Image of Faculty Member
                    var facultyMemberImage = $(this).find('.views-field-field-profile-image-fid #directoryimage a img').attr('src');
                    console.log( facultyMemberImage );

                    // Get Title (Name) of Faculty Member
                    var facultyMemberTitle = $(this).find('.views-field-field-professional-title-value #largetitle').text();
                    console.log( facultyMemberTitle );
                    // Get relative URL fragment.

                    //
                    // Stackoverflow Edit: Much more extraction in this section, see jsFiddle link above.
                    // 

                    // Get Email of Faculty Member
                    var facultyMemberEmail = $(this).find('.views-field-field-email-value span').text();

                    // Simple dashed line to separate faculty members as seen in browser console.
                    console.log('--------');

                    var divObject = '<div class="dynamicResults"><div class="dynamicThumb"><a href="' + facultyMemberUrl + '"><img src="' + facultyMemberImage + '" alt=""></a></div><div class="dynamicInfo"><div class="dynamicText"><a href="' + facultyMemberUrl + '" class="dynamicName">' + facultyMemberTitle + '</a></div><div class="dynamicText">' + facultyMemberPosition + '</div><div class="dynamicText">Phone: ' + facultyMemberPhone + '</div><div class="dynamicText">Location: ' + facultyMemberBuilding + ' <span>' + facultyMemberRoom + '</span></div><div class="dynamicText"><a href="' + facultyMemberEmailUrl + '" class="dynamicEmail">' + facultyMemberEmail + '</a><span class="dynamicEmailpic"></span></div></div></div><div class="clear"></div>';

                    // Build webpage with dynamic data.
                    $('#results').append( divObject );

                });

            });

        }
    }
});

截图: 照片中的缩略图是 100px x 100px修改后的 jsFiddle照片!


但是在真正查看您的问题时,我想尝试一些新的简单的东西……但是结果是可以接受的。这一次,数据抓取技术是使用网页原生CSS文件作为 jsFiddle 中的资产,同时也将返回的数据直接使用到DOM.

此方法使用与上述相同的原理,只是它html用作原始网页.ajax() dataType近克隆。唯一的缺点是需要整个 CSS 文件,但您可以解析原始文件以删除多余的样式和不需要的选择器(重要的是不要打破 IE 中的 4096 CSS 选择器障碍)。

演示:

jsFiddle 数据抓取 HTML:克隆该网页

HTML

<link type="text/css" rel="stylesheet" media="all" href="http://directory.vancouver.wsu.edu/sites/directory.vancouver.wsu.edu/files/css/css_f9f00e4e3fa0bf34a1cb2b226a5d8344.css" />

<div id="facultyAnthropology"></div>

jQuery:

var directoryName = 'anthropology';

    $.ajax({
        type: 'GET',
        url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdirectory.vancouver.wsu.edu%2F"+directoryName+"%22%20and%20xpath%3D%22%2F%2Fdiv%5B%40id%3D'content-area'%5D%22",
        dataType: 'html',
        success: function(data) {
            $('#facultyAnthropology').append($(data).find('results'));
        }
    });

截图: 如上,照片中的缩略图为 100px x 100px

于 2012-12-27T18:23:48.960 回答