Home > Mobile >  How to split text value (from a Scrapy item)?
How to split text value (from a Scrapy item)?

Time:01-24

How to split a text before the last two words or at the place of the comma, to yield two outputs from it?

My code

details = response.xpath('//div[@]')
for detail in details:
    body = detail.xpath('.//ul/li[1]/span[2]/span/text()').get()
    item['body'] = released
    yield item

My output (Column A)

293g (Wi-Fi) / 297g (Wi-Fi Cellular), 6.3mm thickness

Desired output (Column A) | Weight

293g (Wi-Fi) / 297g (Wi-Fi Cellular)

Desired output (Column B) | Dimensions

6.3mm

CodePudding user response:

.split() takes an argument, so you can do this .split(", ") and unpack to weight and thickness.

Try this:

details = response.xpath('//div[@]')
for detail in details:
    weight, thickness = detail.xpath('.//ul/li[1]/span[2]/span/text()').get().split(", ")
    item['weight'] = weight
    item['thickness'] = thickness.split()[0] # gets you 6.3mm only
    yield item
  •  Tags:  
  • Related