【问题标题】:Python & Scrapy output: "\r\n\t\t\t\t\t\t\t"Python & Scrapy 输出:“\r\n\t\t\t\t\t\t\t”
【发布时间】:2020-10-12 12:27:05
【问题描述】:

我正在学习使用 Scrapy 进行抓取,并且在某些代码中遇到了一些问题,这给了我一个我不理解的奇怪输出。有人可以向我解释为什么我得到一堆“\r\n\t\t\t\t\t\t\t”

我在堆栈溢出中找到了这个解决方案: Remove an '\\n\\t\\t\\t'-element from list

但我想知道是什么原因造成的。

这是导致我的问题的代码。上面链接中的 Strip 方法解决了它,但如前所述,我不明白它来自哪里。

import scrapy
import logging
import re

class CitySpider(scrapy.Spider):
    name = 'city'
    allowed_domains = ['www.a-tembo.nl']
    start_urls = ['https://www.a-tembo.nl/themas/category/city/']

    def parse(self, response):
        titles = response.xpath("//div[@class='hikashop_category_image']/a")
        
        for title in titles:
            series = title.xpath(".//@title").get()
            link = title.xpath(".//@href").get()

            #absolute_url = f"https://www.a-tembo.nl{link}"
            #absolute_url = response.urljoin(link)

            yield response.follow(link, callback=self.parse_title)

    def parse_title(self, response):
        rows = response.xpath("//table[@class='hikashop_products_table adminlist table']/tbody/tr")

        for row in rows:
            product_code = row.xpath(".//span[@class='hikashop_product_code']/text()").get()
            product_name = row.xpath(".//span[@class='hikashop_product_name']/a/text()").get()

            yield{
                "Product_code": product_code,
                "Product_name": product_name
                       
            }

【问题讨论】:

    标签: python python-3.x web-scraping scrapy scrapy-shell


    【解决方案1】:

    \n 等字符称为转义字符。 例如: \n 表示一个新行,\t 表示一个制表符。网站上到处都是它们,尽管您在不检查页面的情况下永远不会看到它们。如果你想了解更多关于 Python 中转义字符的信息,可以阅读它们here。我希望这能回答你的问题。

    【讨论】:

    • 感谢您的解释。我并没有真正意识到它来自网站,因为我没有任何迹象表明它在那里。
    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 2017-05-27
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多