用新信息更新CSV

2024-10-03 17:25:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在抓取你买卖玩家(下注)的游戏信息。我想更新一个CSV文件,其中包含关于最终价格的scraped信息。此信息只能在某些“数字”售出后3天内填写(而另一些则为否)。然后,第二次刮我可以得到新的信息,并发送到一个新的CSV。但理想的情况是用一个新列来更新当前的CSV,而不是使用两个单独的文件

到目前为止,我尝试过熊猫,但没有成功。下面的代码,读取每个“FigureID”并每次保存CSV。所以最后,我在Sell列中只有一个值

有什么好主意吗

示例CSV文件

FigureId price date
442124   455   20/05/03
442156   645   20/05/03
442134   235   21/05/03

第二个CSV文件示例

FigureId SellPrice 
442124   830   
442156   Nan
442134   565  

在我的代码中,我将SellPrice和FigureId刮取,然后用meta将其解析为一个新函数。但是,我没有正确地更新SellPrice列(只有最后一个值)//显然我需要创建所有元素的列表。。。但我不知道怎么做

这是到目前为止我的代码

def parse_urls(self, response):
    
    try:
        dateSell_xpath = response.xpath('//*[@id="transHistory"]/table//tr[1]/td[1]/text()').extract_first()
        dateSell = re.sub(r'\s', '', dateSell_xpath)
        dateSold = datetime.strptime(dateSell, '%d-%m-%Y')
        Sold = (self.dateNow - dateSold).total_seconds()* 1.1574e-5

        if Sold < 3:
            price_xpath = response.xpath('//*[@id="transHistory"]/table//tr[1]/td[6]/text()').extract_first()
            priceSell = re.sub(r'\s', '', price_xpath)
            url = str(response.url)
            FigureId= str.split(url,"FigureId=")[1]

            yield scrapy.Request(url, callback=self.parseSells, meta={'IdList':FigureId,'SellList': priceSell}, dont_filter=True)

        else:
            pass

    except:
        dateSell_xpath = None

def parseSells (self, response):
    trsf_item = Item()
    IdList = str(response.meta['IdList'])
    SellList = response.meta['SellList']
    df = pd.read_csv(file)
    df.set_index('FigureId', inplace=True)
    df['FigureId'] = df['FigureId'].astype(str)
    df.loc[IdList,'Sell'] = SellList
    df.to_csv('Test' + file)

Tags: 文件csv代码self信息urldfresponse