<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,Arial,'Hiragino Sans GB','Microsoft YaHei',simsun;vertical-align:baseline;color:rgb(93,93,93);background-color:rgb(255,255,255);">
<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">图片来源:Blake Connally 发布于Unsplash.com<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">简单图片爬虫的原料<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">简单图片爬虫的菜谱<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">以上的所有都安装好了?棒!在我们继续开始写代码前,我先来解释一下以上这些原料都是用来干什么的。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">我们首先要做的是利用 <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:700;line-height:inherit;font-family:inherit;vertical-align:baseline;">Selenium webdriver 和 <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:700;line-height:inherit;font-family:inherit;vertical-align:baseline;">geckodriver 来为我们打开一个浏览器窗口。首先,在 <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:700;line-height:inherit;font-family:inherit;vertical-align:baseline;">Pycharm 中新建一个项目,根据你的操作系统下载最新版的 geckodriver,将其解压并把 geckodriver 文件拖到项目文件夹中。Geckodriver 本质上就是一个能让 Selenium 控制 Firefox 的工具,因此我们的项目需要它来让浏览器帮我们做一些事。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">接下来我们要做的事就是从 Selenium 中导入 webdriver 到我们的代码中,然后连接到我们想爬取的 URL 地址。说做就做:<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">打开浏览器窗口到指定的 URL。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">一个远程控制的 Firefox 窗口。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">相当容易对吧?如果以上所说你都正确完成了,你已经攻克了最难的那部分了,此时你应该看到一个类似于以上图片所示的浏览器窗口。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">滚动页面并等待 5 秒钟。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">下载图片。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">这就是爬取一堆图片所需要做的所有了。很显然的是,除非你想随便找些图片素材来做个设计原型,否则这个小小的爬虫用处可能不是很大。所以我花了点时间来优化它,加了些功能:<ul class="list-paddingleft-2" style="margin-top:1em;margin-bottom:0px;padding-left:30px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,simsun;vertical-align:baseline;list-style-position:outside;color:rgb(93,255);"><li style="margin-top:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;clear:both;"><p style="margin-bottom:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">允许用户通过指定一个命令行参数来指定 <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:700;line-height:inherit;font-family:inherit;vertical-align:baseline;">搜索查询 ,还有一个数值参数指定向下滚动次数,这使得页面可以显示更多的图片可供我们下载。<li style="margin-top:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;clear:both;"><p style="margin-bottom:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">可以自定义的 CSS 选择器。<li style="margin-top:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;clear:both;"><p style="margin-bottom:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">基于搜索查询关键字的自定义 <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:700;line-height:inherit;font-family:inherit;vertical-align:baseline;">结果文件夹 。<li style="margin-top:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;clear:both;"><p style="margin-bottom:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">通过截断图片的预览图链接来获得全 <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:700;line-height:inherit;font-family:inherit;vertical-align:baseline;">高清图片 。<li style="margin-top:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;clear:both;"><p style="margin-bottom:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">基于图片的 URL 给图片文件命名。<li style="margin-top:0px;margin-left:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;clear:both;"><p style="margin-bottom:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;">爬取最终结束后关闭浏览器。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);">你可以(你也应该)尝试自己实现这些功能。全功能版本的爬虫可以在 这里 下载。记得要先按照文章开头所说的,下载 geckodriver 然后连接到你的项目中。<p style="margin-top:1em;margin-bottom:0px;border:0px;font-size:18px;line-height:inherit;font-family:helvetica,255);"><span style="color:rgb(0,0);font-family:Verdana,Helvetica,sans-serif;font-size:14px;text-align:left;background-color:rgb(238,238,238);">欢迎大家加入万人交流群:125240963版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。