String 将整型和浮点型字符串转换为CSV中的单个整型和浮点型

String 将整型和浮点型字符串转换为CSV中的单个整型和浮点型,string,csv,web-scraping,scrapy,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

我在用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循环,但是没有任何效果,我也找不到任何东西。谢谢你的帮助

您可以修复剪贴代码,而不是拆分大量字符串,以便首先将值分隔开

您的项目XPath从//选择与您的规范匹配的所有元素开始,从而在一个(大量)项目中输出所有元素。我想你的目标网站对于目标项目有一些结构,例如表行

然后,您需要找出一个匹配这些行的XPath表达式,并在这些行上循环,以便每行解析一项。请参阅以下伪代码:

def解析(self,response):
#在表行上循环。。。
对于response.xpath('//table/tr')中的sel:
商品=上市前价格()
#在表行中使用XPath开头:在开头使用点
item['volume']=sel.xpath('./td[@class=“tdVolume”]/text()).extract()
# ... 其他领域。。。
收益项目
有关相对XPath表达式的示例,请参见