scrapyでボケてのページの画像を取得するコードを書いています。
その際に次のページに遷移したいので、html5の role="button"の要素を指定したいのですが
取得方法が不明なため、Filtered offsite request となります。
Googleで検索しても対処法が表示されませんので、どなたかご教授いただければと存じます。
ページャーのhtml要素
<div class="pager"> <button class="btn btn-primary" disabled="disabled">1</button> <a href="https://bokete.jp/boke/popular?page=2" class="btn btn-primary">2</a> <a href="https://bokete.jp/boke/popular?page=3" class="btn btn-primary">3</a> <a href="https://bokete.jp/boke/popular?page=4" class="btn btn-primary">4</a> <a href="https://bokete.jp/boke/popular?page=5" class="btn btn-primary">5</a> <a href="https://bokete.jp/boke/popular?page=6" class="btn btn-primary">6</a> <a href="https://bokete.jp/boke/popular?page=7" class="btn btn-primary">7</a> <a href="https://bokete.jp/boke/popular?page=8" class="btn btn-primary">8</a> <a href="https://bokete.jp/boke/popular?page=2" class="btn btn-primary" role="button"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </div>
コード
import scrapy from test2.items import ImageItem class Test2Spider(scrapy.Spider): name = "test2" allowed_domains= "bokete.jp" start_urls = [ 'https://bokete.jp/boke/popular' ] custom_settings = { "DOWNLOAD_DELAY": 1, } def parse(self, response): item = ImageItem() item["image_urls"] = [] for test in response.css('div.boke-photo-content'): item["image_urls"].append(response.urljoin(test.css('img::attr(src)').extract_first())) print(item) #要素指定の方法が不明なため、取り急ぎ8つ目の要素をxpathで指定 next_page = response.xpath('//*[@id="content"]/div[14]/a[8]').css('a::attr(href)').extract_first() print("次のページへ遷移") print(next_page) if next_page is not None: next_page = response.urljoin(next_page) yield scrapy.Request(next_page, callback=self.parse)
ログ
2018-04-18 22:21:47 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://bokete.jp/boke/popular> (referer: None) {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/fcbd84c2e4ade99ba2dd836468652dd3_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/fcbd84c2e4ade99ba2dd836468652dd3_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/ebe477889d6abef800135edea3cc9551_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/fcbd84c2e4ade99ba2dd836468652dd3_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/ebe477889d6abef800135edea3cc9551_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/f6fb9e081d406ad547418f1fd79f4635_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/fcbd84c2e4ade99ba2dd836468652dd3_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/ebe477889d6abef800135edea3cc9551_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/f6fb9e081d406ad547418f1fd79f4635_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/c0e985a74a196129f1d0236c5925d4a5_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/fcbd84c2e4ade99ba2dd836468652dd3_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/ebe477889d6abef800135edea3cc9551_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/f6fb9e081d406ad547418f1fd79f4635_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/c0e985a74a196129f1d0236c5925d4a5_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/8982c95ecdd23d1f71219d77c42b6e02_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/fcbd84c2e4ade99ba2dd836468652dd3_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/ebe477889d6abef800135edea3cc9551_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/f6fb9e081d406ad547418f1fd79f4635_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/c0e985a74a196129f1d0236c5925d4a5_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/8982c95ecdd23d1f71219d77c42b6e02_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/b0261300d199250728d56f694abe9389_600.jpg']} {'image_urls': ['https://d2dcan0armyq93.cloudfront.net/photo/odai/600/9719369b4d431f4110db2254e0c26e50_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/144473544d6e20f6a8c017715fc01431_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/978d15337c221a2265e654f36901ba72_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/fcbd84c2e4ade99ba2dd836468652dd3_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/ebe477889d6abef800135edea3cc9551_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/f6fb9e081d406ad547418f1fd79f4635_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/c0e985a74a196129f1d0236c5925d4a5_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/8982c95ecdd23d1f71219d77c42b6e02_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/b0261300d199250728d56f694abe9389_600.jpg', 'https://d2dcan0armyq93.cloudfront.net/photo/odai/600/547aa98a120169e0ea84f98b9d5ff4ed_600.jpg']} 次のページへ遷移 https://bokete.jp/boke/popular?page=2 2018-04-18 22:21:47 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to 'bokete.jp': <GET https://bokete.jp/boke/popular?page=2> 2018-04-18 22:21:47 [scrapy.core.engine] INFO: Closing spider (finished) 2018-04-18 22:21:47 [scrapy.statscollectors] INFO: Dumping Scrapy stats: {'downloader/request_bytes': 438, 'downloader/request_count': 2, 'downloader/request_method_count/GET': 2, 'downloader/response_bytes': 14460, 'downloader/response_count': 2, 'downloader/response_status_count/200': 2, 'finish_reason': 'finished', 'finish_time': datetime.datetime(2018, 4, 18, 13, 21, 47, 721055), 'log_count/DEBUG': 4, 'log_count/INFO': 7, 'log_count/WARNING': 1, 'memusage/max': 46252032, 'memusage/startup': 46247936, 'offsite/domains': 1, 'offsite/filtered': 1, 'request_depth_max': 1, 'response_received_count': 2, 'scheduler/dequeued': 1, 'scheduler/dequeued/memory': 1, 'scheduler/enqueued': 1, 'scheduler/enqueued/memory': 1, 'start_time': datetime.datetime(2018, 4, 18, 13, 21, 44, 740583)} 2018-04-18 22:21:47 [scrapy.core.engine] INFO: Spider closed (finished)
あなたの回答
tips
プレビュー