【问题标题】:remove the inverted quotes from the scraped data using scrapy in python在python中使用scrapy从抓取的数据中删除倒引号
【发布时间】:2021-08-19 22:38:21
【问题描述】:

我正在尝试在演示网站 quotes.toscrape.com 上使用 Python 中的 Scrapy scrape 数据,然后我尝试将 scraped 数据存储在.csv 文件。

一切正常,但问题是我想从 scraped 数据中修剪倒引号。

注意:我已经尝试使用 extract 方法进行 strip('"'),但它不起作用。下面是我的代码。

def parse(self, response, **kwargs):
    item = ProductsItem()
    data_ = response.css("div.quote")
    for items in data_:
        quote = items.css("span.text::text").extract()
        author = items.css("small.author::text").extract()
        tags = items.css("a.tag::text").extract()
        item['quote'] = quote
        item['author'] = author
        item['tags'] = tags
        yield item

【问题讨论】:

  • strip('"') 不起作用的原因是因为它是一个微妙的不同字符。它们实际上是。您可以使用 strip("\u201c\u201d") 删除这些

标签: python web-scraping scrapy css-selectors


【解决方案1】:

试试这个:

import re

def parse(self, response, **kwargs):
    item = ProductsItem()
    data_ = response.css("div.quote")
    for items in data_:
        quote = items.css("span.text::text").extract()
        author = items.css("small.author::text").extract()
        tags = items.css("a.tag::text").extract()
        item['quote'] = re.sub(r'[^\w\s]','',quote) # This is where I modified the code
        item['author'] = author
        item['tags'] = tags
        yield item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2015-02-05
    • 2013-05-27
    • 1970-01-01
    相关资源
    最近更新 更多