Home > Software design >  How to append specific text to a scrapy item
How to append specific text to a scrapy item

Time:01-23

I would like to add some characters to the text output in scrapy.

Here is my code:

thumbnailname = detail.xpath('.//h1/text()').get().replace(' ','-')

Here is my text output:

Apple-iPhone-13-Pro-Max

How do I get the following output?

My-Apple-iPhone-13-Pro-Max.jpg

Please notice that text should be inserted at the beginning and at the end.

CodePudding user response:

Just concatenate the strings:

thumbnailname = "My-"   detail.xpath('.//h1/text()').get().replace(' ','-')   ".jpg"

CodePudding user response:

you have 2 easy ways to add strings before and after your string:

  1. yourstring = "start{0}end".format(yourstring)

  2. yourstring = "start" yourstring "end"

in your case:

thumbnailname = detail.xpath('.//h1/text()').get().replace(' ','-')
thumbnailname = "My-"   thumbnailname   ".jpg"

or

thumbnailname = "My-{0}.jpg".format(detail.xpath('.//h1/text()').get().replace(' ','-'))"
  •  Tags:  
  • Related