亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Scrapy 輸出 JSON 或 CSV

Scrapy 輸出 JSON 或 CSV

拉風的咖菲貓 2022-10-18 16:04:22
我正在嘗試使用此代碼settings.py進行網絡抓取FEED_EXPORT_ENCODING = 'utf-8'import datetimenow = datetime.datetime.now ()formatted = now.strftime ("%Y%m%d_%H%M")FEED_URI = f'\\C:\\Users\\Acer\\Desktop\\{formatted}.csv'FEED_TYPE = 'csv'有了這個 special_offers.py# -*- coding: utf-8 -*-import scrapyimport datetimeclass SpecialOffersSpider(scrapy.Spider):    name = 'special_offers'    allowed_domains = ['www.tinydeal.com']    def start_requests(self):        yield scrapy.Request(url='https://www.tinydeal.com/specials.html', callback=self.parse, headers={            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'        })    def parse(self, response):        for product in response.xpath("//ul[@class='productlisting-ul']/div/li"):            yield {                'title': product.xpath(".//a[@class='p_box_title']/text()").get(),                'url': response.urljoin(product.xpath(".//a[@class='p_box_title']/@href").get()),                'discounted_price': product.xpath(".//div[@class='p_box_price']/span[1]/text()").get(),                'original_price': product.xpath(".//div[@class='p_box_price']/span[2]/text()").get(),                'User-Agent': response.request.headers['User-Agent'].decode('utf-8'),                'datetime': datetime.datetime.now().strftime("%Y%m%d %H%M")            }        next_page = response.xpath("//a[@class='nextPage']/@href").get()        if next_page:            yield scrapy.Request(url=next_page, callback=self.parse, headers={                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'            })然后我打開終端并使用scrapy crawl special_offers問題是,當我導出 JSON 時,數據在 }{ 之間沒有逗號。例如,使 Power BI 無法讀取我的文件
查看完整描述

1 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

您如何獲取抓取的數據?根據您顯示的內容,我懷疑您是從終端復制的。是這樣嗎?如果是,有一種方法可以使用以下命令將其直接保存到文件中:

scrapy crawl special_offers -o <where save the file>/special_offers.json

希望這可以解決您的問題。請告訴我。


查看完整回答
反對 回復 2022-10-18
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號