Issue
I want to get in Scrapy a numeric value instead of text for json output file. Please see in the code "market_cap" and "price" - they must be in numeric value. How do I do it?
import scrapy
class CoinSpider(scrapy.Spider):
name = "coin"
def start_requests(self):
url = "https://coinmarketcap.com/all/views/all/"
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
for row in response.css("tbody tr"):
yield {
"name": row.css("a.cmc-link::text").extract_first(),
"symbol": row.xpath('td[3]//text()').extract_first(),
"market_cap": row.xpath('td[4]//text()').extract_first(),
"price": row.xpath('td[5]//text()').extract_first()
}
Solution
Before cast it as a int
you need to remove none numeric characters from the string:
"market_cap": int(
''.join(char for char in row.xpath('td[4]//text()').extract_first() if char.isnumeric()))
isnumeric()
returns Boolean:
"a".isnumeric()
False
"2".isnumeric()
True
"$".isnumeric()
False
Answered By - Moein Kameli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.