2

我正在使用scrapy来废弃一个希伯来语网站。然而,即使在将报废的数据编码为 UTF-8 之后,我也无法获得 hewbrew 字符。在 CSV 中获取奇怪的字符串(× ×¨×¡×™ ×'עמ)。但是,如果我检查打印相同的项目,我可以在终端上看到正确的字符串。以下是我正在使用的网站。 http://www.moch.gov.il/rasham_hakablanim/Pages/pinkas_hakablanim.aspx

class Spider(BaseSpider):
    name = "moch"
    allowed_domains = ["www.moch.gov.il"]
    start_urls = ["http://www.moch.gov.il/rasham_hakablanim/Pages/pinkas_hakablanim.aspx"]

    def parse(self, response):

        data = {'ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$cboAnaf': unicode(140),
                'SearchFreeText:': u'חפש',
                'ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$txtShemKablan': u'',
                'ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$txtMisparYeshut': u'',
                'ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$txtShemYeshuv': u'הקלד יישוב',
                'ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$txtMisparKablan': u'',
                'ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$btnSearch': u'חפש',
                'ctl00$ScriptManager1': u'ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$UpdatePanel1|ctl00$ctl13$g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d$ctl00$btnSearch'}

        yield FormRequest.from_response(response,
                                        formdata=data,
                                        callback = self.fetch_details,
                                        dont_click = True)

    def fetch_details(self, response):
        # print response.body
        hxs = HtmlXPathSelector(response)
        item = MochItem()
        names = hxs.select("//table[@id='ctl00_ctl13_g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d_ctl00_gridRashamDetails']//tr/td[2]/font/text()").extract()
        phones = hxs.select("//table[@id='ctl00_ctl13_g_dbcc924d_5066_4fee_bc5c_6671d3e2c06d_ctl00_gridRashamDetails']//tr/td[6]/font/text()").extract()

        index = 0

        for name in names:
            item['name'] = name.encode('utf-8')
            item['phone'] = phones[index].encode('utf-8')
            index += 1
            print item # This is printed correctly on termial. 

            yield item # If I create a CSV output file. Then I am not able to see proper Hebrew String

奇怪的是,如果我在记事本 ++ 中打开相同的 csv。我能够看到正确的输出。所以作为一种解决方法。我所做的是,我在 notepad++ 中打开了 csv 并将编码更改为 UTF-8。并保存了它。现在,当我再次在 excel 中打开 csv 时,它会显示正确的希伯来语字符串。

无论如何从 scrapy 中指定 CSV 编码?

4

0 回答 0