8.1.Crawl実際の使用状況
新プロジェクト
scrapy startprojectのwxapp scrapy genspider -tクロールwxapp_spider " wxapp-union.com "
wxapp_spider.py
#- * -コーディング:UTF-8 - * - 輸入scrapy から scrapy.linkextractorsはインポートLinkExtractorを から scrapy.spiders インポートCrawlSpider、ルールを から wxapp.items インポートWxappItemを クラスWxappSpiderSpider(CrawlSpider): 名前 = ' wxapp_spider ' allowed_domains = [ ' wxapp-union.com ' ] start_urls = [ ' http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1 ' ] ルール = ( ルール(LinkExtractorは(許可 = R '+ MOD =リスト&CATID = \ D ')、続く= TRUE)、 ルール(LinkExtractor(許可 = R ' +記事- 。。。+ \ HTML ')、コールバック= " parse_detail "、フォロー= False)が、 ) デフparse_detail(自己、応答): タイトル = response.xpath(" // H1 /テキスト()[クラス= 'PH' @] " )に.get() author_p = response.xpath(" // P [クラス@ = '著者'] " ) 著者 = author_p.xpath(" .// /テキスト()" )に.get() pub_time = author_p.xpath(" .//スパン/テキスト()" )に.get() article_contentの = response.xpath(" // TD [ID @ = 'article_contentの'] //テキスト()" ).getall() コンテンツ = "" .join(article_contentの).strip() アイテム = WxappItem(タイトル=タイトル、著者=作者、pub_time = pub_time、コンテンツ= コンテンツ) 戻り項目
items.py
#- * -コーディング:UTF-8 - * - 輸入scrapy クラスWxappItem(scrapy.Item): タイトル = scrapy.Field() 著者 = scrapy.Field() pub_time = scrapy.Field() コンテンツ = scrapy.Field()
pipelines.py
#- * -コーディング:UTF-8 - * - scrapy.exporters インポートJsonLinesItemExporterを クラスWxappPipeline(オブジェクト): デフ __init__ (自己): self.fp =オープン(' wxapp.json '、' WB ' ) self.exporter = JsonLinesItemExporter(self.fp、ensure_ascii = Falseを、コード= ' UTF-8 ' ) デフprocess_item(自己、アイテム、クモ): self.exporter.export_item(アイテム) リターン項目 デフclose_spider(自己、クモ): self.fp.close()
settings.py
ROBOTSTXT_OBEY = 偽 DOWNLOAD_DELAY = 1 DEFAULT_REQUEST_HEADERS = { ' 受け入れ':' text / htmlで、アプリケーション/ XHTML + xmlの、アプリケーション/ xmlの; Q = 0.9、* / *; Q = 0.8 ' 、 ' 言語を受け入れる':' エン' 、 ' ユーザエージェント':' Mozillaの/ 5.0(Windows NTの6.1; Win64の、x64の)のAppleWebKit / 537.36(KHTML、ヤモリなど)クローム/ 64.0.3282.140サファリ/ 537.36 ' 、 } ITEM_PIPELINES = { ' wxapp.pipelines.WxappPipeline ':300 、 }
start.py
scrapy インポートCMDLINE cmdline.execute(" scrapyクロールwxapp_spider " .split())
8.1.Crawl実際の使用状況
新プロジェクト
scrapy startprojectのwxapp scrapy genspider -tクロールwxapp_spider " wxapp-union.com "
wxapp_spider.py
#- * -コーディング:UTF-8 - * - 輸入scrapy から scrapy.linkextractorsはインポートLinkExtractorを から scrapy.spiders インポートCrawlSpider、ルールを から wxapp.items インポートWxappItemを クラスWxappSpiderSpider(CrawlSpider): 名前 = ' wxapp_spider ' allowed_domains = [ ' wxapp-union.com ' ] start_urls = [ ' http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1 ' ] ルール = ( ルール(LinkExtractorは(許可 = R '+ MOD =リスト&CATID = \ D ')、続く= TRUE)、 ルール(LinkExtractor(許可 = R ' +記事- 。。。+ \ HTML ')、コールバック= " parse_detail "、フォロー= False)が、 ) デフparse_detail(自己、応答): タイトル = response.xpath(" // H1 /テキスト()[クラス= 'PH' @] " )に.get() author_p = response.xpath(" // P [クラス@ = '著者'] " ) 著者 = author_p.xpath(" .// /テキスト()" )に.get() pub_time = author_p.xpath(" .//スパン/テキスト()" )に.get() article_contentの = response.xpath(" // TD [ID @ = 'article_contentの'] //テキスト()" ).getall() コンテンツ = "" .join(article_contentの).strip() アイテム = WxappItem(タイトル=タイトル、著者=作者、pub_time = pub_time、コンテンツ= コンテンツ) 戻り項目
items.py
#- * -コーディング:UTF-8 - * - 輸入scrapy クラスWxappItem(scrapy.Item): タイトル = scrapy.Field() 著者 = scrapy.Field() pub_time = scrapy.Field() コンテンツ = scrapy.Field()
pipelines.py
#- * -コーディング:UTF-8 - * - scrapy.exporters インポートJsonLinesItemExporterを クラスWxappPipeline(オブジェクト): デフ __init__ (自己): self.fp =オープン(' wxapp.json '、' WB ' ) self.exporter = JsonLinesItemExporter(self.fp、ensure_ascii = Falseを、コード= ' UTF-8 ' ) デフprocess_item(自己、アイテム、クモ): self.exporter.export_item(アイテム) リターン項目 デフclose_spider(自己、クモ): self.fp.close()
settings.py
ROBOTSTXT_OBEY = 偽 DOWNLOAD_DELAY = 1 DEFAULT_REQUEST_HEADERS = { ' 受け入れ':' text / htmlで、アプリケーション/ XHTML + xmlの、アプリケーション/ xmlの; Q = 0.9、* / *; Q = 0.8 ' 、 ' 言語を受け入れる':' エン' 、 ' ユーザエージェント':' Mozillaの/ 5.0(Windows NTの6.1; Win64の、x64の)のAppleWebKit / 537.36(KHTML、ヤモリなど)クローム/ 64.0.3282.140サファリ/ 537.36 ' 、 } ITEM_PIPELINES = { ' wxapp.pipelines.WxappPipeline ':300 、 }
start.py
scrapy インポートCMDLINE cmdline.execute(" scrapyクロールwxapp_spider " .split())