How to remove \r\n when scraping a page?

我与影子孤独终老i 提交于 2019-12-26 13:34:10

问题


I have made a function by scraping a page and I run but the output gives \r\n. I used strip function to remove \r\n but its not working. Why and how to remove \r\n? Here is the link: https://ibb.co/VtVV2fb

import scrapy
from .. items import FetchingItem

class SiteFetching(scrapy.Spider):
    name = 'Site'
    start_urls = ['https://www.rev.com/freelancers']
    transcription_page = 'https://www.rev.com/freelancers/transcription'

    def parse(self, response):
        items = {
            'Heading': response.css('#sign-up::text').extract(),
            'Earn_steps': response.css('.pb2 .lh-copy::text , .mb1::text , .mb3 .lh-copy::text').extract(), 
    }

        yield response.follow(self.transcription_page, self.trans_faqs, meta={'items':items})

    def trans_faqs(self, response):
        items = response.meta['items']
        names = {
            'name1': 'FAQ1',
            'name2': 'FAQ2', 
        }
        finder = {
            'find1': '#whatentailed p::text , #whatentailed .mr3::text',
            'find2': '#requirements p::text , #requirements .mr3::text'
        }
        for name, find in zip(names.values(), finder.values()):
            items[name] = list(map(str.strip,response.css(find).extract()))
        yield items

回答1:


Whenever I have a string with tabs or newlines, I found that replacing them with '' works for me.

For example, if you have both \t and \n in your string variable, you could do this:

string_variable.replace('\n','').replace('\t','')

Works well so far.



来源:https://stackoverflow.com/questions/55979642/how-to-remove-r-n-when-scraping-a-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!