【问题标题】:Strange UnicodeEncodeError/AttributeError in my script我的脚本中出现奇怪的 UnicodeEncodeError/AttributeError
【发布时间】:2016-07-27 12:48:49
【问题描述】:

目前我正在用 Python 2.7 编写一个运行良好的脚本,只是在运行几秒钟后它会遇到错误:

Enter Shopify website URL (without HTTP):  store.highsnobiety.com
Scraping! Check log file @ z:\shopify_output.txt to see output.
!!! Also make sure to clear file every hour or so !!!
Copper Bracelet - 3mm - Polished ['3723603267']
Traceback (most recent call last):
  File "shopify_sitemap_scraper.py", line 38, in <module>
    print(prod, variants).encode('utf-8')
AttributeError: 'NoneType' object has no attribute 'encode'

该脚本用于从 Shopify 网站获取数据,然后将其打印到控制台。代码在这里:

# -*- coding: utf-8 -*-
from __future__ import print_function
from lxml.html import fromstring
import requests
import time
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

# Log file location, change "z://shopify_output.txt" to your location.
logFileLocation = "z:\shopify_output.txt"

log = open(logFileLocation, "w")

# URL of Shopify website from user input (for testing, just use store.highsnobiety.com during input)
url = 'http://' + raw_input("Enter Shopify website URL (without HTTP):  ") + '/sitemap_products_1.xml'

print ('Scraping! Check log file @ ' + logFileLocation + ' to see output.')
print ("!!! Also make sure to clear file every hour or so !!!")
while True :

    page = requests.get(url)
    tree = fromstring(page.content)

    # skip first url tag with no image:title
    url_tags =  tree.xpath("//url[position() > 1]")

    data = [(e.xpath("./image/title//text()")[0],e.xpath("./loc/text()")[0]) for e in  url_tags]

    for prod, url in data:
    # add xml extension to url
        page = requests.get(url + ".xml")
        tree = fromstring(page.content)
        variants = tree.xpath("//variants[@type='array']//id[@type='integer']//text()")
        print(prod, variants).encode('utf-8')

最疯狂的部分是,当我取出 .encode('utf-8') 时,它给了我一个 UnicodeEncodeError 看到这里:

Enter Shopify website URL (without HTTP):  store.highsnobiety.com
Scraping! Check log file @ z:\shopify_output.txt to see output.
!!! Also make sure to clear file every hour or so !!!
Copper Bracelet - 3mm - Polished ['3723603267']
Copper Bracelet - 5mm - Brushed ['3726247811']
Copper Bracelet - 7mm - Polished ['3726253635']
Highsnobiety x EARLY - Leather Pouch ['14541472963', '14541473027', '14541473091']
Traceback (most recent call last):
  File "shopify_sitemap_scraper.py", line 38, in <module>
    print(prod, variants)
  File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\xae' in position 13: character maps to <undefined>'

有什么想法吗?经过数小时的谷歌搜索,不知道还能尝试什么。

【问题讨论】:

    标签: python xml python-2.7 encode


    【解决方案1】:

    snakecharmerb几乎知道了,但错过了第一个错误的原因。你的代码

    print(prod, variants).encode('utf-8')
    

    表示您printprodvariants 变量的值,然后尝试在print 的输出上运行encode() 函数。不幸的是,print()(作为 Python 2 中的函数并且始终在 Python 3 中)返回 None。要修复它,请改用以下内容:

    print(prod.encode("utf-8"), variants)
    

    【讨论】:

    • 使用新代码仍然得到“AttributeError: 'list' object has no attribute 'encode'”
    • @DanielYveson 抱歉,我没有意识到 variants 是一个列表。请参阅上面我编辑的答案。
    【解决方案2】:

    您的控制台默认编码为 cp437,而 cp437 无法表示字符 u'\xae'

    >>> print (u'\xae')
    ®
    >>> print (u'\xae'.encode('utf-8'))
    b'\xc2\xae'
    >>> print (u'\xae'.encode('cp437'))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.5/encodings/cp437.py", line 12, in encode
        return codecs.charmap_encode(input,errors,encoding_map)
    UnicodeEncodeError: 'charmap' codec can't encode character '\xae' in position 0: character maps to <undefined>
    

    您可以在回溯中看到它正在尝试转换为 cp437: File "C:\Python27\lib\encodings\cp437.py", line 12, in encode

    (我在Python3.5中重现了这个问题,但是在两个版本的Python中都是同样的问题)

    【讨论】:

    • 查看@MattDMo 的回答。
    猜你喜欢
    • 2012-05-10
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2018-08-07
    • 1970-01-01
    相关资源
    最近更新 更多