你真的会写爬虫吗?爬虫原理你都懂吗?
1
写在前面的话
咱们直接进入今天的主题---你真的会写爬虫吗?为啥标题是这样,因为我们日常写小爬虫都是一个py文件加上几个请求,但是如果你去写一个正式的项目时,你必须考虑到很多种情况,所以我们需要把这些功能全部模块化,这样也使我们的爬虫更加的健全。
2
基础爬虫的架构以及运行流程
首先,给大家来讲讲基础爬虫的架构到底是啥样子的?JAP君给大家画了张粗糙的图:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556089599549 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
可以添加QQ群1004391443,有飞机大战、颜值打分器、打砖块小游戏、红包提醒神器、小姐姐表白神器等具体的实训项目,有清晰源码,有相应的文件
从图上可以看到,整个基础爬虫架构分为5大类:爬虫调度器、URL管理器、HTML下载器、HTML解析器、数据存储器。
下面给大家依次来介绍一下这5个大类的功能:
- 爬虫调度器,主要是配合调用其他四个模块,所谓调度就是取调用其他的模板
- URL管理器,就是负责管理URL链接的,URL链接分为已经爬取的和未爬取的,这就需要URL管理器来管理它们,同时它也为获取新URL链接提供接口。
- HTML下载器,就是将要爬取的页面的HTML下载下来
- HTML解析器,就是将要爬取的数据从HTML源码中获取出来,同时也将新的URL链接发送给URL管理器以及将处理后的数据发送给数据存储器。
- 数据存储器,就是将HTML下载器发送过来的数据存储到本地
3
实战爬取菜鸟笔记信息
差不多就介绍这么些东西,相信大家对整体的架构有了初步的认识,下面我简单找了个网站给大家演示一遍用爬虫架构来爬取信息:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556089599553 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
(目标站点)
我们来获取上面列表中的信息,这里我就省略了分析网站的一步,如果大家不会分析,可以去看我之前写的爬虫项目。
首先,我们来写一下URL管理器(URLManage.py)
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class URLManager(object): def init(self): self.new_urls = set() self.old_urls = set()
def has_new_url(self): # 判断是否有未爬取的url return self.new_url_size()!=0
def get_new_url(self): # 获取一个未爬取的链接 new_url = self.new_urls.pop() # 提取之后,将其添加到已爬取的链接中 self.old_urls.add(new_url) return new_url
def add_new_url(self, url): # 将新链接添加到未爬取的集合中(单个链接) if url is None: return if url not in self.new_urls and url not in self.old_urls: self.new_urls.add(url)
def add_new_urls(self,urls): # 将新链接添加到未爬取的集合中(集合) if urls is None or len(urls)==0: return for url in urls: self.add_new_url(url)
def new_url_size(self): # 获取未爬取的url大小 return len(self.new_urls)
def old_url_size(self): # 获取已爬取的url大小 return len(self.old_urls)
</pre>
在这里主要就是两个集合,一个是已爬取URL的集合,另一个是未爬取URL的集合。这里我使用的是set类型,因为set自带去重的功能。
接下来,HTML下载器(HTMLDownload.py)
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import requestsclass HTMLDownload(object): def download(self, url): if url is None: return s = requests.Session() s.headers['User-Agent'] ='Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 63.0.3239.132Safari / 537.36' res = s.get(url) # 判断是否正常获取 if res.status_code == 200: res.encoding='utf-8' res = res.text return res return None
</pre>
可以看到这里我们只是简单的获取了,url中的html源码
接着看HTML解析器(HTMLParser.py)
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import refrom bs4 import BeautifulSoupclass HTMLParser(object):
def parser(self, page_url, html_cont): ''' 用于解析网页内容,抽取URL和数据 :param page_url: 下载页面的URL :param html_cont: 下载的网页内容 :return: 返回URL和数据 ''' if page_url is None or html_cont is None: return soup = BeautifulSoup(html_cont, 'html.parser') new_urls = self._get_new_urls(page_url, soup) new_data = self._get_new_data(page_url, soup) return new_urls, new_data
def get_new_urls(self,page_url,soup): ''' 抽取新的URL集合 :param page_url:下载页面的URL :param soup: soup数据 :return: 返回新的URL集合 ''' new_urls = set() for link in range(1,100): # 添加新的url new_url = "http://www.runoob.com/w3cnote/page/"+str(link) new_urls.add(new_url) print(new_urls) return new_urls
def get_new_data(self,page_url,soup): ''' 抽取有效数据 :param page_url:下载页面的url :param soup: :return: 返回有效数据 ''' data={} data['url'] = page_url title = soup.find('div', class='post-intro').find('h2') print(title) data['title'] = title.get_text() summary = soup.find('div', class='post-intro').find('p') data['summary'] = summary.get_text() return data
</pre>
在这里,我们将HTML下载器的源码进行了分析和解析,从而得到了我们想要拿到的数据,如果BeautifulSoup不懂的可以去看一下我之前写的文章。
继续看,数据存储器(DataOutput.py)
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import codecsclass DataOutput(object):
def init(self): self.datas = []
def store_data(self,data): if data is None: return self.datas.append(data)
def output_html(self): fout = codecs.open('baike.html', 'a', encoding='utf-8') fout.write("<html>") fout.write("<head><meta charset='utf-8'/></head>") fout.write("<body>") fout.write("<table>") for data in self.datas: fout.write("<tr>") fout.write("<td>%s</td>"%data['url']) fout.write("<td>《%s》</td>" % data['title']) fout.write("<td>[%s]</td>" % data['summary']) fout.write("</tr>") self.datas.remove(data) fout.write("</table>") fout.write("</body>") fout.write("</html>") fout.close()
</pre>
大家可能发现我这里是将数据存储到一个html的文件当中,在这里你当然也可以存在Mysql或者csv等文件当中,这个看自己的选择,我这里只是为了演示所以就放在了html当中。
最后一个,爬虫调度器(SpiderMan.py)
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from base.DataOutput import DataOutputfrom base.HTMLParser import HTMLParserfrom base.HTMLDownload import HTMLDownloadfrom base.URLManager import URLManager
class SpiderMan(object): def init(self): self.manager = URLManager() self.downloader = HTMLDownload() self.parser = HTMLParser() self.output = DataOutput()
def crawl(self, root_url): # 添加入口URL self.manager.add_new_url(root_url) # 判断url管理器中是否有新的url,同时判断抓取多少个url while(self.manager.has_new_url() and self.manager.old_url_size()<100): try: # 从URL管理器获取新的URL new_url = self.manager.get_new_url() print(new_url) # HTML下载器下载网页 html = self.downloader.download(new_url) # HTML解析器抽取网页数据 new_urls, data = self.parser.parser(new_url, html) print(new_urls) # 将抽取的url添加到URL管理器中 self.manager.add_new_urls(new_urls) # 数据存储器存储文件 self.output.store_data(data) print("已经抓取%s个链接" % self.manager.old_url_size()) except Exception as e: print("failed") print(e) # 数据存储器将文件输出成指定的格式 self.output.output_html()
if name == 'main': spider_man = SpiderMan() spider_man.crawl("http://www.runoob.com/w3cnote/page/1")
</pre>
相信这里大家都能看懂,我就是将前面我们写的四个模板在这里把它们调用了一下,我们运行后的结果:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556089599562 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
4
总结
我们这里简单的讲解了一下,爬虫架构的五个模板,无论是大型爬虫项目还是小型的爬虫项目都离不开这五个模板,希望大家能够照着这些代码写一遍,这样有利于大家的理解,大家以后写爬虫项目也要按照这种架构去写,这样你的爬虫看起来就会更加的规范、健全。