python Scrapy框架原理解析
Python 爬蟲包含兩個重要的部分:正則表達式和Scrapy框架的運用, 正則表達式對于所有語言都是通用的,網絡上可以找到各種資源。
如下是手繪Scrapy框架原理圖,幫助理解
如下是一段運用Scrapy創(chuàng)建的spider:使用了內置的crawl模板,以利用Scrapy庫的CrawlSpider。相對于簡單的爬取爬蟲來說,Scrapy的CrawlSpider擁有一些網絡爬取時可用的特殊屬性和方法:
$ scrapy genspider country_or_district example.python-scrapying.com--template=crawl
運行genspider命令后,下面的代碼將會在example/spiders/country_or_district.py中自動生成。
# -*- coding: utf-8 -*- import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule from example.items import CountryOrDistrictItem class CountryOrDistrictSpider(CrawlSpider): name = 'country_or_district' allowed_domains = ['example.python-scraping.com'] start_urls = ['http://example.python-scraping.com/'] rules = ( Rule(LinkExtractor(allow=r'/index/', deny=r'/user/'), follow=True), Rule(LinkExtractor(allow=r'/view/', deny=r'/user/'), callback='parse_item'), ) def parse_item(self, response): item = CountryOrDistrictItem() name_css = 'tr#places_country_or_district__row td.w2p_fw::text' item['name'] = response.css(name_css).extract() pop_xpath = '//tr[@id="places_population__row"]/td[@class="w2p_fw"]/text()' item['population'] = response.xpath(pop_xpath).extract() return item
爬蟲類包括的屬性:
- name: 識別爬蟲的字符串。
- allowed_domains: 可以爬取的域名列表。如果沒有設置該屬性,則表示可以爬取任何域名。
- start_urls: 爬蟲起始URL列表。
- rules: 該屬性為一個通過正則表達式定義的Rule對象元組,用于告知爬蟲需要跟蹤哪些鏈接以及哪些鏈接包含抓取的有用內容。
以上就是python Scrapy框架原理解析的詳細內容,更多關于Scrapy框架原理的資料請關注腳本之家其它相關文章!
相關文章
Python解析JSON數(shù)據(jù)的基本方法實例代碼
JSON (JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,下面這篇文章主要給大家介紹了關于Python解析JSON數(shù)據(jù)的基本方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-01-01python datatable庫大型數(shù)據(jù)集和多核數(shù)據(jù)處理使用探索
這篇文章主要介紹了python datatable庫大型數(shù)據(jù)集和多核數(shù)據(jù)處理使用探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01python 找出list中最大或者最小幾個數(shù)的索引方法
今天小編就為大家分享一篇python 找出list中最大或者最小幾個數(shù)的索引方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10