本文共 5965 字,大约阅读时间需要 19 分钟。
,里面除了介绍还有很多配置选项。
通用爬虫一般有以下通用特性:
其爬取大量(一般来说是无限)的网站而不是特定的一些网站。
其不会将整个网站都爬取完毕,因为这十分不实际(或者说是不可能)完成的。相反,其会限制爬取的时间及数量。
其在逻辑上十分简单(相较于具有很多提取规则的复杂的spider),数据会在另外的阶段进行后处理(post-processed)
其并行爬取大量网站以避免被某个网站的限制所限制爬取的速度(为表示尊重,每个站点爬取速度很慢但同时爬取很多站点)。
scrapy创建工程是通过命令进行,创建一个名为proname的scrapy工程:
scrapy startproject proname
然后根据提示:
cd proname
然后生成爬虫模板:
scrapy genspider lagou www.lagou.com
通用爬虫的创建过程与默认爬虫创建过程一样,只是在生成爬虫模板的时候命令不同,生成不同的爬虫模板:
scrapy genspider -t crawl lagou www.lagou.com
只需要增加 -t crawl即可。
1.爬虫模板主要使用的类是CrawlSpider,而它继承的是Spider。
2.Spider的入口函数是start_requests()。
3.Spider的默认返回处理函数是parse(),它调用_parse_response(),则允许我们重载parse_start_url()和process_results()方法来对response进行逻辑处理。
4._parse_response()会去调用爬虫模板设置的rules=(Rule(LinkExtractor…)),将response交给LinkExtrator,LinkExtrator会根据我们传进来的参数:
allow=(), deny=(), allow_domains=(), deny_domains=(), restrict_xpaths=(), tags=('a', 'area'), attrs=('href',), canonicalize=False, unique=True, process_value=None, deny_extensions=None, restrict_css=(), strip=True
进行处理,其中deny的意思是除了它以外,反向取值,比如deny=('jobs/')则在处理的时候就会略过jobs,只爬取jobs以外的规则。
在项目目录的spiders文件夹下默认生成proname.py:
import scrapyfrom scrapy.linkextractors import LinkExtractorfrom scrapy.spiders import CrawlSpider, Ruleclass GxrcSpider(CrawlSpider): name = 'proname' allowed_domains = ['www.proname.com'] start_urls = ['http://www.proname.com/'] rules = ( Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True), ) def parse_item(self, response): i = {} return i
1.通过Ctrl + 鼠标左键的方式跟踪CrawlSpider类,发现它是继承Spider的,往下看到CrawlSpider中有个parse方法,那么就意味着后面写代码的时候不能跟之前一样在代码里自定义parse函数了,最好像模板给出的parse_item这种写法。
2.解析parse函数,它调用了_parse_response方法:
def parse(self, response): return self._parse_response(response, self.parse_start_url, cb_kwargs={}, follow=True)
其中的_parse_response可以说是核心函数,里面的参数cb_kwargs代表着参数。通过Ctrl+左键跟进_parse_response:
def _parse_response(self, response, callback, cb_kwargs, follow=True): if callback: cb_res = callback(response, **cb_kwargs) or () cb_res = self.process_results(response, cb_res) for requests_or_item in iterate_spider_output(cb_res): yield requests_or_item if follow and self._follow_links: for request_or_item in self._requests_to_follow(response): yield request_or_item
首先它判断是否有callback,就是parse函数中的parse_start_url方法,这个方法是可以让我们重载的,可以在里面加入想要的逻辑;
然后它还会将参数cb_kwargs传入到callback中,往下看它还调用了process_result方法:
def process_results(self, response, results): return results
这个方法什么都没做,把从parse_start_url接收到的result直接return回去,所以process_result方法也是可以重载的。
接着看:
if follow and self._follow_links: for request_or_item in self._requests_to_follow(response): yield request_or_item
如果存在follow,它就进行循环,跟进_requests_to_follow看一看:
def _requests_to_follow(self, response): if not isinstance(response, HtmlResponse): return seen = set() for n, rule in enumerate(self._rules): links = [lnk for lnk in rule.link_extractor.extract_links(response) if lnk not in seen] if links and rule.process_links: links = rule.process_links(links) for link in links: seen.add(link) r = self._build_request(n, link) yield rule.process_request(r)
在 _requests_to_follow中首先判断是否是response,如果不是就直接返回了,如果是就设置一个set,通过set去重;然后把_rules变成一个可迭代的对象,跟进_rules:
def _compile_rules(self): def get_method(method): if callable(method): return method elif isinstance(method, six.string_types): return getattr(self, method, None) self._rules = [copy.copy(r) for r in self.rules] for rule in self._rules: rule.callback = get_method(rule.callback) rule.process_links = get_method(rule.process_links) rule.process_request = get_method(rule.process_request)
看到:
rule.callback = get_method(rule.callback)rule.process_links = get_method(rule.process_links)rule.process_request = get_method(rule.process_request)
这几个都是前面可以传递过来的,其中rule.process_links是从Rule类中传递过来的:
class Rule(object): def __init__(self, link_extractor, callback=None, cb_kwargs=None, follow=None, process_links=None, process_request=identity): self.link_extractor = link_extractor self.callback = callback self.cb_kwargs = cb_kwargs or {} self.process_links = process_links self.process_request = process_request if follow is None: self.follow = False if callback else True else: self.follow = follow
虽然process_links默认为None,但是实际上我们在需要的时候可以设置的,通常出现在前面爬虫模板代码里面的
Rule(LinkExtractor(allow=r'WebPage/JobDetail.*'), callback='parse_item', follow=True,process_links='links_handle')
然后可以在url那里增加各种各样的逻辑,这里只简单的打印输出:
def links_handle(self, links): for link in links: url = link.url print(url) return links
可以将url进行其他的预处理,比如可以将url拼接到一起、设置不同的url或者对url进行字符切割等操作。(使用举例:常用于大型分城市的站点,比如58的域名nn.58.com、wh.58.com,就可以通过这个对各个站点的域名进行预匹配)
再来到_requests_to_follow方法中看处理逻辑
def _requests_to_follow(self, response): if not isinstance(response, HtmlResponse): return seen = set() for n, rule in enumerate(self._rules): links = [lnk for lnk in rule.link_extractor.extract_links(response) if lnk not in seen] if links and rule.process_links: links = rule.process_links(links) for link in links: seen.add(link) r = self._build_request(n, link) yield rule.process_request(r)
set去重后就yield交给了_build_request处理,build_request则调用_response_downloaded进行页面的下载,下载后的页面交给_parse_response
转载地址:http://nqbsl.baihongyu.com/