基于scrapy實現(xiàn)的簡單蜘蛛采集程序
更新時間:2015年04月17日 12:22:19 作者:pythoner
這篇文章主要介紹了基于scrapy實現(xiàn)的簡單蜘蛛采集程序,實例分析了scrapy實現(xiàn)采集程序的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了基于scrapy實現(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è)計有所幫助。
您可能感興趣的文章:
- 零基礎(chǔ)寫python爬蟲之使用Scrapy框架編寫爬蟲
- 零基礎(chǔ)寫python爬蟲之爬蟲框架Scrapy安裝配置
- Python爬蟲框架Scrapy安裝使用步驟
- python使用scrapy解析js示例
- Python的Scrapy爬蟲框架簡單學(xué)習(xí)筆記
- 深入剖析Python的爬蟲框架Scrapy的結(jié)構(gòu)與運作流程
- 實踐Python的爬蟲框架Scrapy來抓取豆瓣電影TOP250
- Python使用scrapy采集數(shù)據(jù)過程中放回下載過大頁面的方法
- Python打印scrapy蜘蛛抓取樹結(jié)構(gòu)的方法
- 講解Python的Scrapy爬蟲框架使用代理進行采集的方法
相關(guān)文章
以SortedList為例詳解Python的defaultdict對象使用自定義類型的方法
這篇文章主要介紹了以SortedList為例詳解Python的defaultdict對象使用自定義類型的方法,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
訊飛webapi語音識別接口調(diào)用示例代碼(python)
這篇文章主要介紹了如何使用Python3調(diào)用訊飛WebAPI語音識別接口,重點解決了在處理語音識別結(jié)果時判斷是否為最后一幀的問題,通過運行代碼并總結(jié)經(jīng)驗,解決了常見的模塊和屬性錯誤,需要的朋友可以參考下2025-03-03
pandas讀取文件夾下所有excel文件的實現(xiàn)
最近需要做一個需求,要求匯總一個文件夾所有的excel文件,所以本文就來介紹一下pandas讀取文件夾下所有excel文件的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-09-09

