python 爬虫入门解析

版权声明: https://blog.csdn.net/qq_34246164/article/details/83514312

在正式的介绍网络爬虫技术之前,首先按照我自己的学习经历,来说下网络爬虫是什么,需要哪些技术。

(一)什么是网络爬虫

爬虫,按照其字面意思来看,就是一只会爬的虫子。那什么是网络爬虫爬虫呢?没错,就是一支在网络上爬的虫子。只不过这是在网络上爬的虫子,比较特殊,在爬的时候,他还在拾取网络上的数据。

(二)学习网络爬虫需要哪些技能呢?

既然你已经知道什么是网络爬虫了,那么你肯定在想,我怎么样才能制作一只爬虫呢?因此,在正式开始之前,我们一起来做一只小爬虫。

目标网站:就是我们经常用的百度搜索的首页;

目标:把首页的源码下载下来

好了,解析来开始编写抓取程序。请确保,已经安装好python3 和requests 库。

import requests

url = "http://www.baidu.com"

r = requests.get(url)
html = r.text
print(html)

然后让我们运行看下结果:


<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=ç¾åº¦ä¸ä¸ class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ°é»</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>å°å¾</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>è§é¢</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è´´å§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>ç»å½</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç»å½</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">æ´å¤äº§å</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å³äºç¾åº¦</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使ç¨ç¾åº¦åå¿è¯»</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>æè§åé¦</a>&nbsp;京ICPè¯030173å·&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

没错,就是就是这么简单,我们就把百度搜索的首页给抓取下来了。是不是很有成就感?

那么接下来,我们就开始说学习爬虫需要掌握哪些技能。

1、语言及工具的选择

看上面的例子,我们可以看到,首先需要掌握一门合适的语言,这里推荐python, 版本最好是3, 然后是选择一个合适的开发工具,可以使用pycharm,也许入门的时候使用会比较难,但到后面项目管理、编写程序和调试的时候,你就会发现他的好处了。

2、 必要的网络基础

说的只是基础的基础,也就是入门爬虫必须的技能。接下开始介绍其他的技能。

从前面的例子上, 我们可以看到,有一个叫  url 的东东,这个是什么呢?其实看到后面感觉就和平常所说的网址很像,几乎没有任何区别,只不过在在这里它有另外一个高大上的名字,叫网络资源定位符。你也可以这么理解,它就是一个文件的路径,只不过这个路径指向的文件不是放在你的电脑上,而是放在服务器。此外,html,还需要了解一下,知道什么是标签,属性,id,class等,这个部分大约需要花费两三天的时间。

3、 抓取

了解了  url 是什么之后,接下来就是如何通过这个  url   在服务器上抓取数据了

在例子的头部,我们使用import 导入了一个叫 requests 的库, 这个库,也就是我们用来通过url从服务器拿数据的工具。

4、解析

例子上,我们只是把的数据打印了出来,并没有从中提取出数据。因此,为了将数据提取出来,还需要掌握一些提取数据的工具。比如,正则表达式, beautifulsoup 等

5、存储

解析完成后,接下来就是存储了。

存储的工具有很多,比如excel格式的csv, 也可以采用文本存储, 数据库可以使用redis, mongodb, mysql等

6、速度

获取、解析、存储都做完了, 但是速度太慢了,怎么办呢?这个时候就要考虑多进程,多线程,分布式,框架等了

好了,网络爬虫设计到的部分大致就这么多,以后的系列介绍内容,将会围绕这几个部分展开。

每个部分看起来都很少,然后每个部分展开来看都会有很多,比如网页源码的获取,就会涉及到很多种情况,可以先百度,自行了解一下。

猜你喜欢

转载自blog.csdn.net/qq_34246164/article/details/83514312