简体   繁体   中英

Scrapy CSV incorrectly formatted

I'm new to scrapy package, and here's my problem:

import scrapy

class simpleSpider(scrapy.Spider):
    name = "simple_spider"

    start_urls = ['http://quotes.toscrape.com/login']


    def parse(self, response):
        token = response.css("input[name=csrf_token] ::attr(value)").extract_first()
        formdata = {
            'csrf_token' : token,
            'username' : 'rseiji',
            'password' : 'seiji1234'
        }

        yield scrapy.FormRequest(response.url, formdata=formdata, callback=self.parse_logged)

    def parse_logged(self, response):

        yield {
            'text' : response.css('span.text::Text').extract(),
            'author' : response.css('small.author::Text').extract(),
            'tags' : response.css('div.tags a.tag::Text').extract()
        }

This is my spider. And it does work. But when I try to:

scrapy crawl simple_spider -o mySpider.csv

the .csv file doesn't seen to be correctly formated. It extracts only the "text" column.

What's wrong?

Thank you!

Editted: This is my .csv file:

text,author,tags
"“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”,“It is our choices, Harry, that show what we truly are, far more than our abilities.”,“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”,“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”,“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”,“Try not to become a man of success. Rather become a man of value.”,“It is better to be hated for what you are than to be loved for what you are not.”,“I have not failed. I've just found 10,000 ways that won't work.”,“A woman is like a tea bag; you never know how strong it is until it's in hot water.”,“A day without sunshine is like, you know, night.”","Albert Einstein,J.K. Rowling,Albert Einstein,Jane Austen,Marilyn Monroe,Albert Einstein,André Gide,Thomas A. Edison,Eleanor Roosevelt,Steve Martin","change,deep-thoughts,thinking,world,abilities,choices,inspirational,life,live,miracle,miracles,aliteracy,books,classic,humor,be-yourself,inspirational,adulthood,success,value,life,love,edison,failure,inspirational,paraphrased,misattributed-eleanor-roosevelt,humor,obvious,simile"
...

Figure out now that it is not that there are empty columns. The .csv file format is not well defined. Everything came up in just one row!

Solved!

import scrapy

class simpleSpider(scrapy.Spider):
    name = "simple_spider"

    start_urls = ['http://quotes.toscrape.com/login']


    def parse(self, response):
        formdata = {
            'username' : 'rseiji',
            'password' : 'seiji1234'
        }

        yield scrapy.FormRequest.from_response(response, formdata=formdata, callback=self.parse_logged,)


    def parse_logged(self, response):
        # Get list of Selector objects and loop through them
        for quote in response.css('div.quote'):
            # yield each item individually
            yield {
                'text' : quote.css('span.text::Text').extract_first(),
                'author' : quote.css('small.author::Text').extract_first(),
                'author_goodreads_url' : quote.css('span a[href*="goodreads.com"]::attr(href)').extract_first(),
                'tags' : quote.css('div.tags a.tag::Text').extract()
            }

The problem was I was using extract(). What I wanted to do was get a list of Selector objects.

Using extract() will always produce a list output. When you use extract you get a list of strings of the html you requested with the selector, or when using extract_first() a single string. By not using extract() nor extract_first() you create a list of selectors which you can then iterate through and chain a new selector on it allowing you to pickout each individual item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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