最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python網(wǎng)絡(luò)爬蟲 CrawlSpider使用詳解

 更新時間:2019年09月27日 16:55:02   作者:陪伴is最長情的告白  
這篇文章主要介紹了python網(wǎng)絡(luò)爬蟲 CrawlSpider使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

CrawlSpider

  • 作用:用于進(jìn)行全站數(shù)據(jù)爬取
  • CrawlSpider就是Spider的一個子類
  • 如何新建一個基于CrawlSpider的爬蟲文件
    • scrapy genspider -t crawl xxx www.xxx.com
  • 例:choutiPro

LinkExtractor連接提取器:根據(jù)指定規(guī)則(正則)進(jìn)行連接的提取

Rule規(guī)則解析器:將連接提取器提取到的連接進(jìn)行請求發(fā)送,然后對獲取的頁面進(jìn)行指定規(guī)則【callback】的解析

一個鏈接提取器對應(yīng)唯一一個規(guī)則解析器

例:crawlspider深度(全棧)爬取【sunlinecrawl例】

分布式(通常用不到,爬取數(shù)據(jù)量級巨大、時間少時用分布式)

概念:可將一組程序執(zhí)行在多態(tài)機(jī)器上(分布式機(jī)群),使其進(jìn)行數(shù)據(jù)的分布爬取

原生的scrapy框架是否可以實現(xiàn)分布式?

不能

抽屜

# spider文件

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

class ChoutiSpider(CrawlSpider):
  name = 'chouti'
  # allowed_domains = ['www.xxx.com']
  start_urls = ['https://dig.chouti.com/1']

  # 連接提取器:從起始url對應(yīng)的頁面中提取符合規(guī)則的所有連接;allow=正則表達(dá)式
  # 正則為空的話,提取頁面中所有連接
  link = LinkExtractor(allow=r'\d+')
  rules = (
    # 規(guī)則解析器:將連接提取器提取到的連接對應(yīng)的頁面源碼進(jìn)行指定規(guī)則的解析
    # Rule自動發(fā)送對應(yīng)鏈接的請求
    Rule(link, callback='parse_item', follow=True),
    # follow:True 將連接提取器 繼續(xù) 作用到 連接提取器提取出來的連接 對應(yīng)的頁面源碼中
  )
  def parse_item(self, response):
    item = {}
    #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
    #item['name'] = response.xpath('//div[@id="name"]').get()
    #item['description'] = response.xpath('//div[@id="description"]').get()
    return item

陽光熱線網(wǎng)

# 1.spider文件
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from sunLineCrawl.items import SunlinecrawlItem,ContentItem
class SunSpider(CrawlSpider):
  name = 'sun'
  # allowed_domains = ['www.xxx.com']
  start_urls = ['http://wz.sun0769.com/index.php/question/questionType?type=4&page=']

  link = LinkExtractor(allow=r'type=4&page=\d+') # 提取頁碼連接
  link1 = LinkExtractor(allow=r'question/2019\d+/\d+\.shtml') # 提取詳情頁連接
  rules = (
    Rule(link, callback='parse_item', follow=False),
    Rule(link1, callback='parse_detail'),
  )
  # 解析出標(biāo)題和網(wǎng)友名稱數(shù)據(jù)
  def parse_item(self, response):
    tr_list = response.xpath('//*[@id="morelist"]/div/table[2]//tr/td/table//tr')
    for tr in tr_list:
      title = tr.xpath('./td[2]/a[2]/text()').extract_first()
      net_friend = tr.xpath('./td[4]/text()').extract_first()
      item = SunlinecrawlItem()
      item['title'] = title
      item['net_friend'] = net_friend

      yield item

  # 解析出新聞的內(nèi)容
  def parse_detail(self,response):
    content = response.xpath('/html/body/div[9]/table[2]//tr[1]/td/div[2]//text()').extract()
    content = ''.join(content)
    item = ContentItem()
    item['content'] = content

    yield item
--------------------------------------------------------------------------------
# 2.items文件

import scrapy

class SunlinecrawlItem(scrapy.Item):
  title = scrapy.Field()
  net_friend = scrapy.Field()

class ContentItem(scrapy.Item):
  content = scrapy.Field()
--------------------------------------------------------------------------------
# 3.pipelines文件

class SunlinecrawlPipeline(object):
  def process_item(self, item, spider):
    # 確定接受到的item是什么類型(Content/Sunlinecrawl)
    if item.__class__.__name__ == 'SunlinecrawlItem':
      print(item['title'],item['net_friend'])

    else:
      print(item['content'])

    return item
--------------------------------------------------------------------------------
# 4.setting文件

BOT_NAME = 'sunLineCrawl'

SPIDER_MODULES = ['sunLineCrawl.spiders']
NEWSPIDER_MODULE = 'sunLineCrawl.spiders'

LOG_LEVEL = 'ERROR'

USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'

ROBOTSTXT_OBEY = False

ITEM_PIPELINES = {
  'sunLineCrawl.pipelines.SunlinecrawlPipeline': 300,
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

五指山市| 镇安县| 乐山市| 天水市| 哈巴河县| 额尔古纳市| 安乡县| 金乡县| 宁陵县| 于都县| 夹江县| 科尔| 云霄县| 定结县| 固安县| 芦溪县| 利津县| 丹棱县| 许昌市| 汶上县| 资兴市| 来宾市| 永清县| 岳普湖县| 平遥县| 方山县| 元氏县| 新宾| 习水县| 隆安县| 东乡族自治县| 武冈市| 日照市| 肇东市| 噶尔县| 茌平县| 外汇| 沽源县| 大丰市| 临桂县| 丁青县|