基于scrapy實(shí)現(xiàn)的簡單蜘蛛采集程序
更新時間:2015年04月17日 12:22:19 作者:pythoner
這篇文章主要介紹了基于scrapy實(shí)現(xiàn)的簡單蜘蛛采集程序,實(shí)例分析了scrapy實(shí)現(xiàn)采集程序的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實(shí)例講述了基于scrapy實(shí)現(xiàn)的簡單蜘蛛采集程序。分享給大家供大家參考。具體如下:
# Standard Python library imports # 3rd party imports from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.selector import HtmlXPathSelector # My imports from poetry_analysis.items import PoetryAnalysisItem HTML_FILE_NAME = r'.+\.html' class PoetryParser(object): """ Provides common parsing method for poems formatted this one specific way. """ date_pattern = r'(\d{2} \w{3,9} \d{4})' def parse_poem(self, response): hxs = HtmlXPathSelector(response) item = PoetryAnalysisItem() # All poetry text is in pre tags text = hxs.select('//pre/text()').extract() item['text'] = ''.join(text) item['url'] = response.url # head/title contains title - a poem by author title_text = hxs.select('//head/title/text()').extract()[0] item['title'], item['author'] = title_text.split(' - ') item['author'] = item['author'].replace('a poem by', '') for key in ['title', 'author']: item[key] = item[key].strip() item['date'] = hxs.select("http://p[@class='small']/text()").re(date_pattern) return item class PoetrySpider(CrawlSpider, PoetryParser): name = 'example.com_poetry' allowed_domains = ['www.example.com'] root_path = 'someuser/poetry/' start_urls = ['http://www.example.com/someuser/poetry/recent/', 'http://www.example.com/someuser/poetry/less_recent/'] rules = [Rule(SgmlLinkExtractor(allow=[start_urls[0] + HTML_FILE_NAME]), callback='parse_poem'), Rule(SgmlLinkExtractor(allow=[start_urls[1] + HTML_FILE_NAME]), callback='parse_poem')]
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- 零基礎(chǔ)寫python爬蟲之使用Scrapy框架編寫爬蟲
- 零基礎(chǔ)寫python爬蟲之爬蟲框架Scrapy安裝配置
- Python爬蟲框架Scrapy安裝使用步驟
- python使用scrapy解析js示例
- Python的Scrapy爬蟲框架簡單學(xué)習(xí)筆記
- 深入剖析Python的爬蟲框架Scrapy的結(jié)構(gòu)與運(yùn)作流程
- 實(shí)踐Python的爬蟲框架Scrapy來抓取豆瓣電影TOP250
- Python使用scrapy采集數(shù)據(jù)過程中放回下載過大頁面的方法
- Python打印scrapy蜘蛛抓取樹結(jié)構(gòu)的方法
- 講解Python的Scrapy爬蟲框架使用代理進(jìn)行采集的方法
相關(guān)文章
python實(shí)現(xiàn)抖音點(diǎn)贊功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)抖音點(diǎn)贊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04