简体   繁体   English

Python - 如何格式化 csv 文件中的数据?

[英]Python - How do I format scrapy data in a csv file?

I am new to python and web scraping and I tried storing the scrapy data to a csv file however the output is not satisfactory.我是 python 和 web 抓取的新手,我尝试将抓取的数据存储到 csv 文件中,但是输出并不令人满意。

Current csv output:当前 csv 输出:

Title             Image
Audi,Benz,BMW     Image1,Image2,Image3

how i would like to view it in a csv file:我想如何在 csv 文件中查看它:

Title     Image
Audi      Image1
Benz      Image2
BMW       Image3

this is what is type in the terminal to run it:这是在终端中键入以运行它的内容:

scrapy crawl testscraper -t csv -o test.csv

Here's the spider.py:这是spider.py:

class TestSpiderSpider(scrapy.Spider):
name = 'testscraper'
page_number = 2
start_urls = ['https://jamaicaclassifiedonline.com/auto/cars/']

    def parse(self, response):
    items = scrapeItem()

    product_title = response.css('.jco-card-title::text').extract()
    product_imagelink = response.css('.card-image img::attr(data-src)').getall()

    items['product_title'] = product_title
    items['product_imagelink'] = product_imagelink
    items.append('items')

    yield items

He's the code for items.py:他是 items.py 的代码:

class scrapeItem(scrapy.Item):
product_title = scrapy.Field()
product_imagelink = scrapy.Field()

pass

You can select every div element that contains a car and then iterate over those elements, yielding them one by one.您可以选择包含汽车的每个 div 元素,然后遍历这些元素,一一生成它们。

def parse(self, response):

        for car in response.css('.col.l3.s12.m6'):
            item = scrapeItem()

            product_title = car.css('.jco-card-title::text').get()
            product_imagelink = car.css('.card-image img::attr(data-src)').get()

            # Some of the elements don't contain a title or a image_link, like ads for example.
            if product_title and product_imagelink:

                item['product_title'] = product_title.strip().replace('\n', '')
                item['product_imagelink'] = product_imagelink

                yield item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM