如何删除新行的重复?

时间:2019-05-09 21:14:42

标签: python scrapy

当我运行这段代码时,它给了我一个字,然后离开了整行,此过程一直重复到最后。我希望它位于row中,而不是columns中。

import scrapy
from .. items import FetchingItem
import re

class SiteFetching(scrapy.Spider):
    name = 'Site'

    def start_requests(self):
        links = [('https://www.rev.com/freelancers/subtitles', self.parse_subtitles)]

        return [scrapy.Request(link[0], callback=link[1]) for link in links]

    def parse_subtitles(self, response):
        subtitles = FetchingItem()
        Subtitles_price = response.css('.rate::text , .language::text').extract()
        Subtitles_price = [re.sub(r'[\r\n\s]+', ' ', text) for text in Subtitles_price]

        subtitles['Subtitles_price'] = Subtitles_price

        yield subtitles

1 个答案:

答案 0 :(得分:0)

str.strip()不能工作吗?像这样:

subtitles_price = [text.strip() for text in subtitles_price]

它可能有多个价格,在这种情况下,您可以使用:

subtitles_price = ", ".join([text.strip() for text in subtitles_price])

您还可以使用str.lstrip()str.rstrip()仅从字符串的开头或结尾删除换行符