python - 使用 Scrapy 将数据收集到 CSV 文件中
问题描述
我正在学习如何使用Scrapy
蜘蛛.py
import scrapy
class TestSetSpider(scrapy.Spider):
name = "test_spider"
start_urls = ['https://example.html']
def parse(self, response):
for brickset in response.xpath('//div[@class="product-name"]'):
yield {
'name': brickset.xpath('h1/text()').extract_first(),
}
我用命令运行这个蜘蛛:scrapy crawl test_spider -o test.csv
这适用于//div[@class="product-name"
,但我不知道如何在同一个蜘蛛文件中添加另一个 CSS/XPath 类
我正在尝试这个,但它不起作用
import scrapy
class TestSetSpider(scrapy.Spider):
name = "test_spider"
start_urls = ['https://example.html']
def parse(self, response):
for test in response.xpath('//div[@class="product-name"]'):
yield {
'name': test.xpath('h1/text()').extract_first(),
}
def parse(self, response):
for attempt in response.xpath('//div[@class="another-class"]'):
yield {
'color': attempt.xpath('h1/a/text()').extract_first(),
}
请帮助我做到这一点。
解决方案
def parse(self, response):
product_name_lst = []
# we will append all data to product_name_lst
for test in response.xpath('//div[@class="product-name"]'):
product_name_lst.append('name': test.xpath('h1/text()').extract_first())
another_product_name_lst = []
# we will append all data to another_product_name_lst
for test in response.xpath('//div[@class="another-product-name"]'):
another_product_name_lst.append('name': test.xpath('h1/text()').extract_first())
# after that write to out.csv all the data you need from
# product_name_lst and another_prodct_name_lst lists
out_file = open('out.csv', 'a') # a meen append to file not rewrite file
# and here you need to write in out.csv file
out.write(data) # data is what you need to write
# and close the file
out.close()
推荐阅读
- r - R(dplyr 包)- 每组至少一行(随机抽样,每组 10% 行)
- node.js - 如何在特定通道中发送错误消息
- java - 请帮我解释一下,当这些符号组合在一起时,我知道这意味着什么
- python - Django-在 django-admin 中显示记录,包括组、最大计数和时间戳
- php - PHP - 通过 mail() 从表单发送带有附件的电子邮件
- linux - blktrace 和交换线程
- c++ - 有没有办法只强制临时实例化?
- javascript - 在 vue.js 中 clearInterval 不使用
- timestamp - 如何在不同的计算机上准确记录同步时间?
- node.js - Mongoose 中间件 schema.pre('save', ...)