【问题标题】:Python's Scrapy encoding issuePython 的 Scrapy 编码问题
【发布时间】:2016-04-04 02:25:26
【问题描述】:

我正在尝试使用 Scrapy 从该站点进行抓取:http://www.fs.fed.us/research/people/profile.php?alias=ggonzalez

这是返回我在蜘蛛中导出的最终项目的函数:

def parse_post(self, response):
    theitems = []
    pubs = response.xpath("//div[@id='pubs']/ul/li/a")
    for i in pubs:
        item = FspeopleItem()
        name = str(response.xpath("//div[@id='maincol']/h1/text() | //nobr/text()").extract()).strip()
        pub = str(i.xpath("@title").extract()).strip() 
        item['link'] = response.url
        item['name'] = name
        item['pub'] = pub
        theitems.append(item)
    return theitems

由于某种原因,返回的“theitems”总是将重音字符(如 Díaz 中的 í)显示为空格。我不知道为什么会这样。当我打开一个 Scrapy shell 并与 xpath 分开打印信息时,它可以很好地打印到控制台,但是当它从返回的“theitems”中出来时,它变成了一个空白区域。我已经在 Python2.7 和 3.5 中测试过它。

我是 Scrapy 的新手,一般是编码,一般是 python。但是,除了这个编码问题之外,一切正常。有谁知道为什么会这样?

谢谢。

//////编辑////////

感谢您的建议。虽然格式更好,因为当我使用以下代码时 /u' 东西消失了(通过使用

.encode("utf-8")

.extract_first()

在撰写我的项目时),带有口音的字符仍然很时髦。因此,我查看了我正在抓取的网站上的编码,发现它们使用的是 ISO-8859-1 编码。所以我尝试了

.encode("ISO-8859-1")

向项目添加组件时,当我打开 .csv 时,这正确显示了带有重音符号等的字符(所有格式都很棒)。然而,当我这样做时,大约 25% 的网站没有被抓取——csv 有 ~1400 个条目而不是 ~2100 个。我不明白为什么它不抓取某些网站而不抓取其他网站?

import scrapy

from fspeople.items import FspeopleItem

class FSSpider(scrapy.Spider):
name = "hola"
allowed_domains = ["fs.fed.us"]
start_urls = [
    "http://www.fs.fed.us/research/people/people_search_results.php?employeename=&keywords=&station_id=SRS&state_id=ALL"]

def __init__(self):
    self.i = 0

def parse(self,response):
    for sel in response.xpath("//a[@title='Click to view their profile ...']/@href"):
        url = response.urljoin(sel.extract())
        yield scrapy.Request(url, callback=self.parse_post)
    self.i += 1

def parse_post(self, response):
    theitems = []
    pubs = response.xpath("//div[@id='pubs']/ul/li")
    for i in pubs:
        item = FspeopleItem()
        name = response.xpath("//div[@id='maincol']/h1/text() | //nobr/text()").extract_first().strip().encode("ISO-8859-1")
        pubname = i.xpath("a/text()").extract_first().strip().encode("ISO-8859-1")
        pubauth = i.xpath("text()").extract_first().strip().encode("ISO-8859-1")

        item['link'] = response.url
        item['name'] = name
        item['pubname'] = pubname
        item['pubauth'] = pubauth
        theitems.append(item)
    return theitems

【问题讨论】:

  • 当你说“总是显示重音字符”时,你用什么软件来显示输出,你用什么输出格式?

标签: python encoding scrapy


【解决方案1】:

使用extract_first()encode()

for i in pubs:
    item = FspeopleItem()
    name = response.xpath("//div[@id='maincol']/h1/text() | //nobr/text()").extract_first().strip().encode("utf-8")
    pub = i.xpath("@title").extract_first().strip().encode("utf-8") 
    item['link'] = response.url
    item['name'] = name
    item['pub'] = pub
    theitems.append(item)

【讨论】:

  • alecxe,我仍然遇到问题,尽管您的评论已经部分解决了问题。我刚刚对原始帖子的底部进行了编辑。如果站点是“ISO-8859-1”,我应该使用 .encode("ISO-8859-1"),对吗?但是,现在这不会刮掉所有条目。 .encode("uff-8") 会抓取所有条目,但会在 csv 中奇怪地呈现特殊字符。
【解决方案2】:

这是一个编码/解码问题。

正如史蒂夫所说,它可能只是您用来查看提取数据的软件。

如果不是这样,请尝试删除 str() 方法,看看会发生什么。或者将其更改为unicode() [1]。我通常不使用它们,我只是让该字段填充来自response.xpath('...').extract()的任何内容。

此外,请确保您的项目中的所有内容都是 utf8:您编写代码、设置和字符串的文件。例如,永远不要写它:

item['name'] = 'First name: ' + name

写这个(unicode!):

item['name'] = u'First name: ' + name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2011-07-02
    相关资源
    最近更新 更多