簡體   English   中英

Scrapy CSV 文件格式不正確

[英]Scrapy CSV file is in the incorrect format

基本上,我將提取的這些數據放入 csv 文件中,但格式存在一些問題。

- 首先只顯示零件,其他沒有顯示 fg。 數量和價格 - 其次,列標題似乎重復了行。

我希望將零件、價格、數量顯示在不同的列中,並且標題將是名稱。 如果有人能告訴我在哪里可以學習這樣做,那將有很大幫助!

    name = 'digi'
    allowed_domains = ['digikey.com']
    custom_settings = {
        "USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"


    }
    start_urls = ['https://www.digikey.com/products/en/integrated-circuits-ics/memory/774?FV=-1%7C428%2C-8%7C774%2C7%7C1&quantity=0&ColumnSort=0&page=1&k=cy621&pageSize=500&pkeyword=cy621']

    def parse(self, response):
        data={}
        parts=response.css('Table#productTable.productTable')
        for part in parts:
            for p in part.css('tbody#lnkPart'):
                yield {
                    'Part': p.css('td.tr-mfgPartNumber span::text').extract(),
                    'Quantity': p.css('td.tr-minQty.ptable-param span.desktop::text').extract(),
                    'Price': p.css('td.tr-unitPrice.ptable-param span::text').extract()
                }

設置

BOT_NAME = 'website1'

SPIDER_MODULES = ['website1.spiders']
NEWSPIDER_MODULE = 'website1.spiders'

#Export as CSV Feed
#FEED_EXPORT_FIELDS: ["parts", "quantity", "price"]
FEED_FORMAT = "csv"
FEED_URI = "parts.csv"

# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'website1 (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

在 Scrapy shell 中進行測試時,您得到的數據是否正確? 在將它們提交到腳本之前,值得在 scrapy shell 中嘗試您的選擇器。

我沒有詳細查看您的 CSS 選擇器,但是有很多 for 循環,基本上您需要做的就是遍歷 tr。 因此,找到一個 CSS 選擇器來獲取所有行而不是遍歷整個表並按照自己的方式向下工作可能更有效。

更新:

既然你問了 for 循環

for p in response.css('tbody#lnkPart > tr'):
        
       yield {
                'Part': p.css('td.tr-mfgPartNumber span::text').get(),
                'Quantity': p.css('td.tr-minQty.ptable-param span.desktop::text').get(),
                'Price': p.css('td.tr-unitPrice.ptable-param span::text').get()
       }

請注意,我們只需要在 tr 周圍循環,這會選擇所有這些。 get() 方法僅選擇具有特定 tr 的項目。

請注意,您需要考慮如何處理空間和無項目。 值得仔細考慮這部分並想出一種簡單的方法來修改結果。

更新代碼

def parse(self, response):

    for p in response.css('tbody#lnkPart > tr'):
    
        if p.css('td.tr-minQty.ptable-param span.desktop::text').get(): 
            quantity = p.css('td.tr-minQty.ptable-param span.desktop::text').get()
            quantity = quantity.strip()
            cleaned_quantity = int(quantity.replace(',',''))
        else:
            quantity = 'No quantity'
     
        if p.css('td.tr-unitPrice.ptable-param span::text').get():
            price = p.css('td.tr-unitPrice.ptable-param span::text').get()
            cleaned_price = price.strip()
        else: 
            price = 'No Price'
        yield {
                'Part': p.css('td.tr-mfgPartNumber span::text').get(),
                'Quantity': cleaned_quantity,
                'Price': cleaned_price
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM