{"id":21073,"date":"2023-06-06T08:29:14","date_gmt":"2023-06-05T23:29:14","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=21073"},"modified":"2023-06-07T22:18:58","modified_gmt":"2023-06-07T13:18:58","slug":"%e3%80%90python%e3%80%91scrapy%e3%81%aecrawl%e3%83%86%e3%83%b3%e3%83%97%e3%83%ac%e3%83%bc%e3%83%88%e3%81%a7%e5%ae%9f%e8%a3%85","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=21073","title":{"rendered":"\u3010Python\u3011scrapy\u306ecrawl\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u5b9f\u88c5"},"content":{"rendered":"<h2>crawl\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u306f\uff1f<\/h2>\n<p>\u901a\u5e38\u306eWeb\u30b5\u30a4\u30c8\u3092\u30af\u30ed\u30fc\u30eb\u3059\u308b\u305f\u3081\u306espider\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u306a\u308a\u307e\u3059\u3002\u30eb\u30fc\u30eb\u3092\u5b9a\u7fa9\u3057\u3066\u30ea\u30f3\u30af\u3092\u8fbf\u3063\u3066\u3044\u304d\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306ebasic\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u6bd4\u3079\u3066\u30ea\u30f3\u30af\u3092\u305f\u3069\u308a\u3084\u3059\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>Basic\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u306e\u9055\u3044<\/h3>\n<p>Basic\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u3082\u540c\u3058\u3088\u3046\u306b\u30af\u30ed\u30fc\u30eb\u3059\u308b\u3053\u3068\u306f\u53ef\u80fd\u3067\u3059\u3002\u305f\u3060\u3001Basic\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u9055\u3044\u30af\u30ed\u30fc\u30eb\u3059\u308b\u305f\u3081\u306e\u30eb\u30fc\u30eb\u3092\u8a18\u8ff0\u3059\u308b\u3060\u3051\u3067\u81ea\u52d5\u7684\u306b\u30af\u30ed\u30fc\u30eb\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u7d30\u304b\u3044\u6319\u52d5\u306e\u6307\u5b9a\u306f\u4e0d\u8981\u306b\u306a\u308b\u3068\u3044\u3046\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u57fa\u672c\u7684\u306b\u306f\u4f8b\u3048\u3070\u3001\u540c\u3058\u30da\u30fc\u30b8\u304b\u3089\u8907\u6570\u306e\u8a73\u7d30\u30da\u30fc\u30b8\u3078\u9077\u79fb\u3059\u308b\u3068\u3044\u3046\u3088\u3046\u306a\u30da\u30fc\u30b8\u9077\u79fb\u304c\u591a\u3044\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u306e\u5834\u5408\u306fBasic\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3088\u308a\u3082crawl\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u65b9\u304c\u512a\u308c\u3066\u3044\u308b\u3068\u8a00\u3048\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>spider\u306e\u4f5c\u6210<\/h2>\n<pre class=\"lang:default decode:true \">scrapy genspider -t crawl spider\u540d URL(https:\/\/\u306a\u3057\u306e)<\/pre>\n<h2>\u751f\u6210\u3055\u308c\u305fSpider<\/h2>\n<p>\u4ee5\u4e0b\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u304cspider\u30d5\u30a9\u30eb\u30c0\u306b\u751f\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import scrapy\r\nfrom scrapy.linkextractors import LinkExtractor\r\nfrom scrapy.spiders import CrawlSpider, Rule\r\n\r\n\r\nclass XXXSpider(CrawlSpider):\r\n    name = \"xxx\"\r\n    allowed_domains = [\"www.xxx.co.jp\"]\r\n    start_urls = [\"https:\/\/www.xxx.co.jp\/f\/dsd-101001037028005-06-\"]\r\n\r\n    rules = (Rule(LinkExtractor(allow=r\"Items\/\"), callback=\"parse_item\", follow=True),)\r\n\r\n    def parse_item(self, response):\r\n        item = {}\r\n        #item[\"domain_id\"] = response.xpath('\/\/input[@id=\"sid\"]\/@value').get()\r\n        #item[\"name\"] = response.xpath('\/\/div[@id=\"name\"]').get()\r\n        #item[\"description\"] = response.xpath('\/\/div[@id=\"description\"]').get()\r\n        return item\r\n<\/pre>\n<h3>rules\u5c5e\u6027<\/h3>\n<p>basic\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u4e3b\u306b\u7570\u306a\u3063\u3066\u3044\u308b\u70b9\u306b\u306a\u308a\u307e\u3059\u3002tuple\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u5f8c\u3067\u5909\u66f4\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002Rule\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u6301\u3057\u3066\u3044\u307e\u3059\u3002tuple\u306a\u306e\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8907\u6570\u306eRule\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u6301\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002\u8907\u6570\u6307\u5b9a\u3057\u305f\u5834\u5408\u306f\u4e0a\u304b\u3089\u9806\u756a\u306b\u30eb\u30fc\u30eb\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">rules = (\r\n  Rule(LinkExtractor(...)),\r\n  Rule(LinkExtractor(...))\r\n)<\/pre>\n<h4>Rule\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h4>\n<p>\u6307\u5b9a\u3057\u305f\u30b5\u30a4\u30c8\u306b\u304a\u3044\u3066\u3069\u306e\u3088\u3046\u306b\u30ea\u30f3\u30af\u3092\u8fbf\u3063\u3066\u3044\u304f\u304b\u3092\u6307\u5b9a\u3059\u308b\u3082\u306e\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u4e00\u89a7\u753b\u9762\u306e\u4e2d\u306e\u8a73\u7d30\u753b\u9762\u306eXPath\u3092\u6307\u5b9a\u3057\u3066\u3042\u3052\u308c\u3070\u5168\u3066\u306e\u8a73\u7d30\u30da\u30fc\u30b8\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h4>LinkExtractor(allow=r\"Items\/\")<\/h4>\n<p>\u30af\u30ed\u30fc\u30eb\u3055\u308c\u305f\u5404\u307a\u30fc\u3058\u304b\u3089\u30ea\u30f3\u30af\u3092\u62bd\u51fa\u3059\u308b\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<p>\u30ea\u30f3\u30af\u3092\u8fbf\u308b\u304b\u3069\u3046\u304b\u3092allow\u304bdeny\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002r\u306f\u6b63\u898f\u8868\u73fe\u3067\u3053\u306e\u5834\u5408\u306f\u300cItems\/\u300d\u3068\u3044\u3046\u30ea\u30f3\u30af\u3092\u8fbf\u3063\u3066\u3044\u304f\u3068\u3044\u3046\u6307\u5b9a\u306b\u306a\u308a\u307e\u3059\u3002URL\u306e\u6307\u5b9a\u65b9\u6cd5\u306f\u4ed6\u306b\u3082\u3044\u308d\u3044\u308d\u3042\u308a\u307e\u3059\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>allow=r\"Items\/\"<\/td>\n<td>URL\u3092\u6b63\u898f\u8868\u73fe\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>restrict_xpaths=('xpath')<\/td>\n<td>URL\u3092XPath\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u00a0restrict_css='css\u30bb\u30ec\u30af\u30bf'<\/td>\n<td>\u00a0URL\u3092CSS\u30bb\u30ec\u30af\u30bf\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>callback=\"parse_item\"<\/h4>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u30e1\u30bd\u30c3\u30c9\u3067\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u8fd4\u3063\u3066\u304d\u305f\u3089\u30ec\u30b9\u30dd\u30f3\u30b9\u3068\u3057\u3066\u5b9f\u884c\u3057\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u306fparse_item\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\uff08\u306a\u304a\u3001\u6ce8\u610f\u70b9\u3068\u3057\u3066\u306f\u30e1\u30bd\u30c3\u30c9\u306b\u306fparse\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u5229\u7528\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002crawl\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5185\u3067\u6697\u9ed9\u3067\u5229\u7528\u3055\u308c\u308b\u30e1\u30bd\u30c3\u30c9\u540d\u306e\u305f\u3081\u3067\u3059\u3002\uff09<\/p>\n<h4>follow=True<\/h4>\n<p>rules\u3067\u5b9a\u7fa9\u3057\u305f\u30ea\u30f3\u30af\u5148\u306e\u3055\u3089\u306b\u30ea\u30f3\u30af\u5148\u3092\u8fbf\u308b\u304b\u3069\u3046\u304b\u6307\u5b9a\u3059\u308bboolean\u578b\u3067\u3059\u3002true\u306a\u3089\u30ea\u30f3\u30af\u3092\u8fbf\u308b\u6307\u5b9a\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3\u3092\u305f\u3069\u308a\u3064\u3064\u5404\u8a73\u7d30\u30da\u30fc\u30b8\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b\u30b5\u30f3\u30d7\u30eb<\/h2>\n<pre class=\"lang:default decode:true \">    rules = (Rule(LinkExtractor(restrict_xpaths='\u4e00\u89a7\u753b\u9762\u306e\u8a73\u7d30\u30ea\u30f3\u30af\u306eXPath'), \r\n             callback=\"parse_item\", follow=False),\r\n             Rule(LinkExtractor(restrict_xpaths='\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3\u6b21\u3078\u306e\u30ea\u30f3\u30af\u306eXPath,\r\n             )\r\n\r\n    def parse_item(self, response):\r\n        logging.info(response.url)\r\n        yield {\r\n            '\u8a73\u7d30\u60c5\u58311': response.xpath('\u8a73\u7d30\u60c5\u58311\u306expath').get(),\r\n            '\u8a73\u7d30\u60c5\u58312': response.xpath('\u8a73\u7d30\u60c5\u58312\u306expath').get(),\r\n        }<\/pre>\n<ol>\n<li>\u4e00\u89a7\u753b\u9762\u306e1\u30da\u30fc\u30b8\u76ee\u306e\u8a73\u7d30\u753b\u9762\u306e\u30ea\u30f3\u30af\u3092\u5168\u3066\u8aad\u307f\u8fbc\u3080\uff0820\u4ef6\u3042\u308c\u307020\u4ef6\u5168\u3066\u8aad\u307f\u8fbc\u3080\uff09<\/li>\n<li>\u6b21\u3078\u30dc\u30bf\u30f3\u30ea\u30f3\u30af\u3092\u30af\u30ea\u30c3\u30af<\/li>\n<\/ol>\n<p>\u3068\u3044\u3046\u51e6\u7406\u306e\u6d41\u308c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"crawl\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u306f\uff1f \u901a\u5e38\u306eWeb\u30b5\u30a4\u30c8\u3092\u30af\u30ed\u30fc\u30eb\u3059\u308b\u305f\u3081\u306espider\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u306a\u308a\u307e\u3059\u3002\u30eb\u30fc\u30eb\u3092\u5b9a\u7fa9\u3057\u3066\u30ea\u30f3\u30af\u3092\u8fbf\u3063\u3066\u3044\u304d\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306ebasic\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u6bd4\u3079\u3066\u30ea\u30f3\u30af\u3092\u305f\u3069\u308a\u3084\u3059\u304f\u306a\u3063\u3066\u3044\u307e\u3059 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[47],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21073"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21073"}],"version-history":[{"count":23,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21073\/revisions"}],"predecessor-version":[{"id":21106,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/21073\/revisions\/21106"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21073"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}