2016-04-06 12 views
0

Derzeit ich ein Skript in Python schreibe 2.7, die mit Ausnahme funktioniert gut für nachdem er für einige Sekunden läuft es läuft in einen Fehler:Seltsam UnicodeEncodeError/Attribute in meinem Skript

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' 

Das Skript ist, Daten zu erhalten von einer Shopify-Website und drucken Sie es dann auf der Konsole. Code hier:

# -*- 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') 

Der verrückteste daran ist, dass, wenn ich die .encode('utf-8') nehmen es mir ein UnicodeEncodeError hier zu sehen gibt:

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>' 

Irgendwelche Ideen? Hab keine Ahnung, was ich nach stundenlangem Googlen noch versuchen sollte.

Antwort

1

snakecharmerbfast habe es, aber verpasste die Ursache für Ihren ersten Fehler. Ihr Code

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

bedeutet, dass Sie print die Werte des prod und variants Variablen, dann versuchen, die encode() Funktion am Ausgang der print auszuführen. Leider gibt print() (als eine Funktion in Python 2 und immer in Python 3) None zurück. Um es zu beheben, verwenden Sie stattdessen Folgendes:

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

Immer noch die "AttributeError: 'Liste' Objekt hat kein Attribut 'encode'" mit dem neuen Code –

+0

@DanielYveson Sorry, ich habe nicht erkannt, dass 'Varianten' eine Liste war. Siehe meine bearbeitete Antwort oben. – MattDMo

+0

Funktioniert wunderbar, danke. –

1

Ihre Konsole hat eine Standardcodierung von cp437, und cp437 kann das Zeichen u'\xae' nicht darstellen.

>>> 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> 

Sie können sehen, dass es in der Rückverfolgungs zu CP437 zu konvertieren versucht: File "C:\Python27\lib\encodings\cp437.py", line 12, in encode

(ich das Problem in Python3.5 reproduziert, aber es ist das gleiche Problem in beiden Versionen von Python)

+0

Jeder Weg, es zu lösen? –

+0

Siehe @ MattDMos Antwort. – snakecharmerb