抓取网页时如何从输出中剥离\ n?

时间:2019-06-06 01:03:09

标签: python web-scraping scrapy splash-screen scrapy-splash

我正在抓取一个网页,当我得到一个结果时,除了我的卡名列之外,其他所有内容看起来都很好,因为我在卡名之前得到了\n。如何防止输出?

    # Scraping
    def parse(self, response):
        item = GameItem()

        item["Category"] = response.css("span.titletext::text").extract()
        for game in response.css("tr[class^=deckdbbody]"):
            item["card_name"] = game.css("a.card_popup::text").extract_first()
            if item["card_name"] != None:
                saved_name = item["card_name"]
            else:
                item["card_name"] = saved_name

            item["Condition"] = game.css("td[class^=deckdbbody].search_results_7 a::text").get()
            item["stock"] = game.css("td[class^=deckdbbody].search_results_8::text").extract_first()
            item["Price"] = game.css("td[class^=deckdbbody].search_results_9::text").extract_first()

            yield item

样本输出

{"Category": ["Duel Decks: Venser vs. Koth"], "card_name": "\nAether Membrane", "Condition": "NM/M", "stock": "93", "Price": "$0.59"},
{"Category": ["Duel Decks: Venser vs. Koth"], "card_name": "\nAether Membrane", "Condition": "PL", "stock": "59", "Price": "$0.49"},
{"Category": ["Duel Decks: Venser vs. Koth"], "card_name": "\nAngelic Shield", "Condition": "NM/M", "stock": "35", "Price": "$0.25"},
{"Category": ["Duel Decks: Venser vs. Koth"], "card_name": "\nAnger", "Condition": "NM/M", "stock": "9", "Price": "$1.49"},
{"Category": ["Duel Decks: Venser vs. Koth"], "card_name": "\nAnger", "Condition": "PL", "stock": "49", "Price": "$1.19"},

2 个答案:

答案 0 :(得分:4)

内置字符串方法strip()str.strip())删除所有可打印的字符。

答案 1 :(得分:1)

这应该做到。

# Scraping
def parse(self, response):
    item = GameItem()

    item["Category"] = response.css("span.titletext::text").extract()
    for game in response.css("tr[class^=deckdbbody]"):
        item["card_name"] = game.css("a.card_popup::text").extract_first()

        # strip added here ->
        if item["card_name"] != None:
            saved_name = item["card_name"].strip()
        else:
            item["card_name"] = saved_name

        item["Condition"] = game.css("td[class^=deckdbbody].search_results_7 a::text").get()
        item["stock"] = game.css("td[class^=deckdbbody].search_results_8::text").extract_first()
        item["Price"] = game.css("td[class^=deckdbbody].search_results_9::text").extract_first()

        yield item

如果可能的话,一个不相关的代码简化技巧:

# Scraping
def parse(self, response):
    item = GameItem()

    # declare saved_name before hand to avoid NameError
    saved_name = ""

    item["Category"] = response.css("span.titletext::text").extract()
    for game in response.css("tr[class^=deckdbbody]"):

        # simplify conditional statement

        saved_name = game.css("a.card_popup::text").extract_first() or saved_name
        item["card_name"] = saved_name.strip()

        item["Condition"] = game.css("td[class^=deckdbbody].search_results_7 a::text").get()
        item["stock"] = game.css("td[class^=deckdbbody].search_results_8::text").extract_first()
        item["Price"] = game.css("td[class^=deckdbbody].search_results_9::text").extract_first()

        yield item