将Ints和Floats的字符串转换为CSV中的单个Ints和浮点

时间:2017-05-06 18:59:47

标签: string csv web-scraping scrapy

我使用scrapy来刮取股票上市前数据。以下是用于抓取网站的代码:

def parse(self, response):
    for sel in response.xpath('//body'):
        item = PremarketItem()
        item['volume'] = sel.xpath('//td[@class="tdVolume"]/text()').extract()
        item['last_price'] = sel.xpath('//div[@class="lastPrice"]/text()')[:30].extract()
        item['percent_change'] = sel.xpath(
        '//div[@class="chgUp"]/text()')[:15].extract() + sel.xpath('//div[@class="chgDown"]/text()')[:15].extract()
        item['ticker'] = sel.xpath('//a[@class="symbol"]/text()')[:30].extract()
        yield item

将以下代码输出到.csv文件中的内容如下:

ticker,percent_change,last_price,volume
"HTGM,SNCR,SAEX,IMMU,OLED,DAIO","27.43%,20.39%,17.28%,17.19%,15.69%","5,298350,700,1090000,76320,27190,13010",etc

正如您所看到的,这些值是正确分开的,但它们都存在于大量字符串中。我尝试了多个for循环,但没有任何工作,我找不到任何东西。谢谢你的帮助!

0 个答案:

没有答案