Bilal Khan
Bilal Khan

Reputation: 17

How to remove \r\n from output in Scrapy in command prompt?

I am scraping pages of a website. Whenever I try to scrape Regional_subcategories. I get \r\n with many spaces. How to remove it?

Here is the image: image

Here is my code :

def Regional_category(self, response):
    names = {'name1':'Regional_subcategories',
             'name2':'Related_Categories',
             'name3':'Site title',
             'name4':'Site Description',
             }
    finder = {'finder1': '.browse-node::text',
              'finder2': '.one-browse-node::text',
              'finder3': '.site-title::text',
              'finder4': '.site-descr::text',
              }
    yield from self.find_items(response, names, finder)

Upvotes: 1

Views: 1102

Answers (1)

Cloudomation
Cloudomation

Reputation: 1662

Try str.strip:

>>> s = '\r\n      text\r\n    '
>>> print(s.strip())
text

if you have a list of strings from which you want to remove whitespace you can use list comprehension:

>>> li = ['\r\n      text\r\n    ', '\r\n      text2\r\n    ']
>>> li2 = [st.strip() for st in li]
>>> li2
['text', 'text2']

Upvotes: 3

Related Questions