我正在尝试使用此代码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
希望这可以解决您的问题。请告诉我。
添加回答
举报
0/150
提交
取消