crawler

简介

爬虫可分为几大模块:

  • URL管理器:管理已经下载与未下载的网页URL(内存、数据库、缓存数据库)
  • 下载器:通过url下载网页内容;urllib2(Python3中改名urllib)、request(更强大)
  • 解析器:通过一定规则将下载器所得内容解析获取有价值信息;BeautifulSoup(html.parser、lxml )、正则
调度程序

爬虫的控制中心,宏观上控制整个爬虫逻辑
spider_main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-

"""
@author: Hodge
@func: 爬虫调度程序
@time: 2018/3/21 12:00
"""

from baike_spider import url_manager, html_downloader, html_parser, html_outputer


class SpiderMain(object):
def __init__(self):
self.urls = url_manager.UrlManager()
self.download = html_downloader.HtmlDownloader()
self.parser = html_parser.HtmlParser()
self.outputer = html_outputer.HtmlOutputer()

def craw(self, root_url):
count = 1
self.urls.add_new_url(root_url)
while self.urls.has_new_url():
try:
new_url = self.urls.get_new_url
print("craw %d : %s" % (count, new_url))
html_cont = self.download.download(new_url)
new_urls, new_data = self.parser.parser(new_url, html_cont)
self.urls.add_new_urls(new_urls)
self.outputer.collect_data(new_data)
count += 1

if count == 20:
break
except:
print("craw failed")
self.outputer.output_html()


if __name__ == '__main__':
root_url = "https://baike.baidu.com/item/Python/407313"
obj_spider = SpiderMain()
obj_spider.craw(root_url)

下载器

通过给定的URL获取对应的网页源代码,并处理编码问题
html_downloader.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-

"""
@author: Hodge
@file: html_downloader.py
@time: 2018/3/21 12:01
"""
import string
from urllib import request
from urllib.parse import quote


class HtmlDownloader(object):
def download(self, url):
if url is None:
return None

url_ = quote(url, safe=string.printable)
response = request.urlopen(url_)

if response.getcode() != 200:
return None

return response.read().decode("utf-8")


if __name__ == '__main__':
hd = HtmlDownloader()
html = hd.download("https://baike.baidu.com/item/Python/407313")
print(html)

解析器

根据下载器获取的网页源码,解析出下一次爬取的目标网页urls和当前页面的有价值信息
html_parser.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-

"""
@author: Hodge
@file: html_parser.py
@time: 2018/3/21 12:01
"""
import re
import urllib.parse as urlparse
from bs4 import BeautifulSoup


class HtmlParser(object):
def parser(self, page_url, html_cont):
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):
new_urls = set()
links = soup.find_all('a', href=re.compile(r"/item/"))
for link in links:
new_url = link['href']
new_full_url = urlparse.urljoin(page_url, new_url)
new_urls.add(new_full_url)
return new_urls

def _get_new_data(self, page_url, soup):
res_data = {'url': page_url}
# <dd class="lemmaWgt-lemmaTitle-title"> <h1>Python</h1>
title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title").find('h1')
res_data['title'] = title_node.get_text()

summary_node = soup.find('div', class_="lemma-summary")
res_data['summary'] = summary_node.get_text()

return res_data

url管理器

管理已经下载与未下载的网页URLs,同时负责url的拼接等操作。
url_manager.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-

"""
@author: Hodge
@file: url_manager.py
@time: 2018/3/21 12:01
"""


class UrlManager(object):
def __init__(self):
self.new_urls = set()
self.old_urls = set()

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 has_new_url(self):
return len(self.new_urls) != 0

@property
def get_new_url(self):
new_url = self.new_urls.pop()
self.old_urls.add(new_url)
return new_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)

结果输出

将解析的有价值数据格式化输出保存到指定文件,注意编码问题。
html_outputer.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-

"""
@author: Hodge
@file: html_outputer.py
@time: 2018/3/21 12:02
"""


class HtmlOutputer(object):
def __init__(self):
self.datas = []

def collect_data(self, data):
if data is None:
return
self.datas.append(data)

def output_html(self):
fout = open('output.html', 'w', encoding='utf-8')
fout.write("<html>")
fout.write("<body>")
fout.write("<a>")

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>")
fout.write('<a href="%s">%s</a>' % (data['url'], data['title']))
fout.write('<p>%s</p>' % data['summary'])

fout.write("</a>")
fout.write("</body>")
fout.write("</html>")

fout.close()

这只是最简单基础的爬虫结构,将来可在此基础上针对性扩展。