###■経緯
シンプルなScrapyのプログラムを組んで、複数URL分をCSV出力してみたところ、結果が以下のように重複するようになりました。他のWebページ/URLでも複数指定されると同現象が発生します。単一のURLからCSV出力した場合は正常のようです。
###■質問
0. 回答者様の環境で同じHTMLファイルとPythonファイルでCSV出力した時、同現象は再現しますでしょうか。
0. 現象の改善方法、記述ミスの発生している可能性が高いポイントをお教えいただけますでしょうか。
###■詳細
※HTMLファイルは、番号違いで計3つ存在しています。test1.html 〜 test3.html
HTML
1<html> 2<head> 3 <title>test2</title> <!-- 番号違いで test1〜3が存在 --> 4</head> 5 <body> 6 <table> 7 <tbody> 8 <tr> 9 <td>test2 A1</td> <!-- 番号違いで test1〜3が存在 --> 10 <td>test2 B1</td> <!-- 番号違いで test1〜3が存在 --> 11 </tr> 12 </tbody> 13 </table> 14 </body> 15</html>
Scrapy
1$ scrapy crawl test -o test.csv
PythonScrapySpider
1import scrapy 2from ..items import TestItem 3 4class TestSpider(scrapy.Spider): 5 name = 'test' 6 start_urls = [ 7 'file:///Users/MyName/Desktop/tutorial/test1.html', 8 'file:///Users/MyName/Desktop/tutorial/test2.html', 9 'file:///Users/MyName/Desktop/tutorial/test3.html', 10 ] 11 12 def parse(self, response): 13 for url in self.start_urls: 14 table_rows = response.xpath('//table/tbody/tr') 15 16 for table_row in table_rows: 17 item = TestItem() 18 item['test_01'] = table_row.xpath('td[1]/text()').extract_first() 19 item['test_02'] = table_row.xpath('td[2]/text()').extract_first() 20 21 yield item 22
####【現象1】3つのURLを指定した場合: 9行のレコードが出る
'file:///Users/MyName/Desktop/tutorial/test1.html',
'file:///Users/MyName/Desktop/tutorial/test2.html',
'file:///Users/MyName/Desktop/tutorial/test3.html',
CSV
1test_01,test_02 2test1 A1,test1 B1 3test1 A1,test1 B1 4test1 A1,test1 B1 5test2 A1,test2 B1 6test2 A1,test2 B1 7test2 A1,test2 B1 8test3 A1,test3 B1 9test3 A1,test3 B1 10test3 A1,test3 B1 11
####【現象2】2つのURLを指定した場合: 4行のレコードが出る
'file:///Users/MyName/Desktop/tutorial/test1.html',
'file:///Users/MyName/Desktop/tutorial/test2.html',
CSV
1test_01,test_02 2test1 A1,test1 B1 3test1 A1,test1 B1 4test2 A1,test2 B1 5test2 A1,test2 B1
####【現象3】1つのURLを指定した場合: 1行のレコードが出る ※正常
'file:///Users/MyName/Desktop/tutorial/test1.html',
CSV
1test_01,test_02 2test1 A1,test1 B1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。