加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

资深程序员为大家带来30分钟Python 爬虫教程!能看懂就能学会!

发布时间:2020-12-17 01:20:19 所属栏目:Python 来源:网络整理
导读: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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读