0
私はウェブクローラの処理を使用しており、データをcsvファイルにロードしています。私はxpathを使用していますが、私のforループの間違いから生じると思われるデータを適切に読み込む問題に遭遇しました。それは、最初のタイトル、著者と引用を各ページから抽出して、3行のcsvファイルを生成します。これは私の初めてのpythonを使用して、私は適切に列挙/ zip関数を実装するのに苦労しています。ここPython - ページごとに1回のみループするスクラップされたデータを生成するループ
import scrapy
class MySpider(scrapy.Spider):
name = 'test'
custom_settings = {
'FEED_FORMAT': 'csv',
'FEED_URI': 'test.csv'
}
start_urls = [
'http://quotes.toscrape.com/',
'http://quotes.toscrape.com/page/2/',
'http://quotes.toscrape.com/page/3/'
]
def parse(self, response):
titles = response.xpath("//div[contains(@class, 'col-md-4')]/h2/text()").extract()
authors = response.xpath("//small[contains(@class, 'author')]/text()").extract()
quotes = response.xpath("//div[contains(@class, 'quote')]/span[contains(@class, 'text')]/text()").extract()
for i, (title, author, quote) in enumerate(zip(titles, authors, quotes)):
yield {'index': i, 'title': title, 'author': author, 'quote': quote}