【发布时间】:2016-01-24 11:44:45
【问题描述】:
我正在编写一个基于 selenium phantomjs 的通用蜘蛛来访问和抓取网页。
程序的输入包括需要爬取的模板(css选择器),输出应根据模板生成数据。
如果我们尝试从网站抓取图片,有时可能会得到空图片(如果页面源到执行时不包含图片,就会出现这种情况),可以通过wait 解决
然而,当网页为图像提供占位符时会出现更具挑战性的问题,这些占位符后来通过ajax 请求替换为真实图像 URL。
问题是,如何确保 selenium 仅在图像的真实 URL 包含在页面中时才抓取图像。我正在考虑检查图像的src 属性是否有更改,并且只有在单次更改后我才应该开始解析页面源。但是,不确定如何实施?或者如果这是一个好主意?
编辑
<html>
<head>
<style>
img {
width: 100%;
height: auto;
}
</style>
</head>
<body>
<div id='wrapper'>
<div class='wrapper-child'>
<img data-backup='./1clr.jpg' src='./1bw.jpg'>
</div>
<div class='wrapper-child'>
<img data-backup='./2clr.jpg' src='./2bw.jpg'>
</div>
<div class='wrapper-child'>
<img data-backup='./3clr.jpg' src='./3bw.jpg'>
</div>
</div>
<script src='./jquery.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
// setTimeout(function() {
//replace image placeholders
$.get("ajax/test.html", function(data) {
}).always(function() {
$('img').each(function() {
$(this).attr('src', $(this).attr('data-backup'));
});
});
// }, 1000);
});
</script>
</body>
</html>
假设我有这个页面,我如何在 jquery 更新后使用 selenium 来抓取图像?
【问题讨论】:
标签: python selenium web-crawler phantomjs wait