欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于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ì)有所幫助。

相關(guān)文章

  • Python之 requests的使用(一)

    Python之 requests的使用(一)

    requests是一個很實(shí)用的Python HTTP客戶端庫,爬蟲和測試服務(wù)器響應(yīng)數(shù)據(jù)時經(jīng)常會用到,requests是Python語言的第三方的庫,專門用于發(fā)送HTTP請求,使用起來比urllib簡潔很多,這篇文章主要介紹requests的基礎(chǔ)用法
    2023-04-04
  • 基于python 等頻分箱qcut問題的解決

    基于python 等頻分箱qcut問題的解決

    這篇文章主要介紹了基于python 等頻分箱qcut問題的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python可視化tkinter詳解

    Python可視化tkinter詳解

    這篇文章主要介紹了Python可視化tkinter詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Python?切片為什么不會索引越界?

    Python?切片為什么不會索引越界?

    這篇文章主要介紹了Python?切片為什么不會索引越界?切片(slice)是?Python?中一種很有特色的特性,在正式開始之前,我們先來從關(guān)于切片的相關(guān)知識開始介紹,感興趣的小伙伴一起參考參考呀</P><P>
    2021-12-12
  • python切片復(fù)制列表的知識點(diǎn)詳解

    python切片復(fù)制列表的知識點(diǎn)詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于python切片復(fù)制列表的知識點(diǎn)相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。
    2021-10-10
  • python實(shí)現(xiàn)抖音點(diǎn)贊功能

    python實(shí)現(xiàn)抖音點(diǎn)贊功能

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)抖音點(diǎn)贊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python使用jpype的踩坑記錄

    Python使用jpype的踩坑記錄

    Pype是一個能夠讓 python 代碼方便地調(diào)用 Java 代碼的工具,這篇文章主要來和大家分享一下Python使用jpype會踩的一些坑,希望對大家有所幫助
    2023-06-06
  • Python 從attribute到property詳解

    Python 從attribute到property詳解

    這篇文章主要介紹了Python 從attribute到property詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 詳解django中使用定時任務(wù)的方法

    詳解django中使用定時任務(wù)的方法

    在本篇文章中我們給大家介紹了關(guān)于django中使用定時任務(wù)的方法的相關(guān)知識點(diǎn),有需要的朋友們參考下。
    2018-09-09
  • python中使用urllib2偽造HTTP報頭的2個方法

    python中使用urllib2偽造HTTP報頭的2個方法

    這篇文章主要介紹了python中使用urllib2偽造HTTP報頭的2個方法,即偽造http頭信息,需要的朋友可以參考下
    2014-07-07

最新評論