【发布时间】:2017-05-06 18:59:46
【问题描述】:
我正在使用 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 循环,但没有任何效果,而且我找不到任何东西。感谢您的帮助!
【问题讨论】:
标签: string csv web-scraping scrapy