首页 > 解决方案 > 使用 SCRAPY 抓取特殊字符

问题描述

我正在用丹麦语刮一页。我在输出时遇到问题。输出包含许多特殊字符,例如(Ã¥, Ã, Ã¥, æ),它与页面上的不同。

我怎样才能像在页面上一样刮掉文字?

示例链接:https ://novaindex.com/dk/leverandoerer/mode-og-tekstiler/arbejdstoej

import scrapy
    
class MainSpider(scrapy.Spider):
    name = 'main'

    start_urls = ['https://novaindex.com/dk/leverandoerer/mode-og-tekstiler/arbejdstoej']

    def parse(self, response):

        details = response.xpath('//a[@class="companyresult "]')

        for each in details:
            name = each.xpath('normalize-space(.//span[@class="name"]/text())').get()
            street = each.xpath('normalize-space(.//span[@class="street"]/text())').get()
            city = each.xpath('normalize-space(.//span[@class="city"]/text())').get()
            phone = each.xpath('normalize-space(.//span[@class="phone"]/text())').get()

            yield {
                "Name": name,
                "Street Address": street,
                "City Address": city,
                "Phone": phone,
            }

标签: pythonweb-scrapingscrapy

解决方案


您可以在.encode('utf8')之后添加get()getall()

Scrapy 将数据提取为 unicode 字符串,这可以帮助您了解 unicode 和 UTF-8。

什么是 unicode 字符串?


推荐阅读