我最近完成了一个包含几个 jsFiddles 的教程,并解释了如何使用YQL
、XPATH
和 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