scrapyクローラフレーム(5)-CrawlSpider

scrapyクローラフレーム(5)-CrawlSpider

CrawlSpiderリンク抽出することで、クロールの記事マイクロ手紙アプレットのコミュニティ

マーク

ファイル爬虫類を作成します。

フォルダ爬虫類に入る前に、使用するcdテンプレート(TEMPL)爬虫類を作成し、コマンドを、そして

scrapy genspider -t crawl 爬虫名 网站域名
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from wxapp.items import WxappItem
class WxappspiderSpider(CrawlSpider):
    name = 'wxappSpider'
    allowed_domains = ['wxapp-union.com']
    start_urls = ['http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1']

    rules = (
        Rule(LinkExtractor(allow=r'.+mod=list&catid=2&page=\d'),  follow=True),
        Rule(LinkExtractor(allow=r'.+article-.+\.html'), callback="parse_detail", follow=True)
    )

    def parse_detail(self, response):
        title = response.xpath("//h1[@class='ph']/text()").get()
        links = response.xpath("//p[@class='authors']")
        author = links.xpath(".//a/text()").get()
        time = links.xpath(".//span[@class='time']//text()").getall()
        article = response.xpath("//td[@id='article_content']//text()").getall()
        article = "".join(article).strip()
        item = WxappItem(title=title, author=author, time=time, article=article)
        yield item

業績

マーク

おすすめ

転載: www.cnblogs.com/senup/p/12321418.html