Home > Back-end >  Trying to split text from title
Trying to split text from title

Time:03-05

I want to remove these from my output: I want these only Wave Coffee Collection

'\n\n\t\t3rd Wave Coffee Collection\n\t\t\t\t\n\t'

This is my code :

from scrapy.http import Request
import scrapy
class PushpaSpider(scrapy.Spider):
    name = 'pushpa'
    start_urls = ['https://onepagelove.com/inspiration']
    

    def parse(self, response):
        books = response.xpath("//div[@class='thumb-image']//a//@href").extract()
        for book in books:
            absolute_url = response.urljoin(book)
            yield Request(absolute_url, callback=self.parse_book)

    def parse_book(self, response):
        title = response.xpath("//span[@class='review-name']//h1//text()").extract_first()
        


        yield{
            'title':title
            }

            

CodePudding user response:

If this is your resulting output:

result = '\n\n\t\t3rd Wave Coffee Collection\n\t\t\t\t\n\t'

Then you can easily achieve your desired output like this:

result = result.strip()

CodePudding user response:

You can use the .replace() function:

input = '\n\n\t\t3rd Wave Coffee Collection\n\t\t\t\t\n\t'

input = input.replace('\n','').replace('\t','')

print(input)

  • Related