【问题标题】:While scraping the website through scrapy in python getting the following error:在python中通过scrapy抓取网站时出现以下错误:
【发布时间】:2018-05-11 23:05:36
【问题描述】:

错误:UnicodeEncodeError:“charmap”编解码器无法编码字符 u'\u201c' 在位置 0:字符映射到

代码:# -- 编码:utf-8 --

import scrapy


class Spider1Spider(scrapy.Spider):
    name = 'spider_1'
    allowed_domains = ["quotes.toscrape.com"]
    start_urls = (
        'http://quotes.toscrape.com/' ,
        )

    def parse(self, response):
     x=response.xpath('//*[@class="quote"]')
     for quotes in x:
        text= x.xpath('.//*[@class="text"]/text()').extract_first()
        author= x.xpath('.//*[@class="author"]/text()').extract_first()
        Tags= x.xpath('.//*[@class="keywords"]/@content').extract_first()

        print '\ n'
        print text
        print author
        print Tags
        print '\ n'

问题:如果我只使用extract_first,那么它会抛出错误,否则我们只使用提取。它工作得很好。

任何人都可以帮忙,因为我是编程世界的新手并期待积极的解决方案。

【问题讨论】:

  • 2.7 unicode有很多问题,试试python 3.x

标签: python-2.7 scrapy


【解决方案1】:

您需要在打印之前.encode() Unicode 字符串:

print text.encode('utf-8')
print author.encode('utf-8')
print Tags.encode('utf-8')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多