2017-02-22 2 views
0

Der Versuch, Daten von Website-HTML mit Lxml und XPath zu bekommen. Es scheint in Ordnung, außer es gibt etwas Unpassendes:Python-Parser gibt Symbole anstelle von Wörtern aus html zurück

from lxml import html 
import requests 

page = requests.get('https://karabas.com') 
tree = html.fromstring(page.content) 

test_info = tree.xpath('//div[@class="posters-top"]/a/text()') 

print(test_info) 

Dies ist ein Stück html:

<div class="posters-top"> 
<a href="https://kiev.karabas.com/didyulya" class="category" style="background-colo #FF7A05;">Концерты</a> 
</div> 

Porgramm zurück:

[u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'8 \u043c\u0430\u0440\u0442\u0430', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'8 \u043c\u0430\u0440\u0442\u0430', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'8 \u043c\u0430\u0440\u0442\u0430', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'8 \u043c\u0430\u0440\u0442\u0430', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'8 \u043c\u0430\u0440\u0442\u0430', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0443\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0443\u0440\u044b', u'\u0421\u0435\u043c\u0438\u043d\u0430\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'8 \u043c\u0430\u0440\u0442\u0430', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0443\u0440\u044b', u'\u041f\u043e\u044d\u0437\u0438\u044f', u'\u0421\u043f\u043e\u0440\u0442', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0443\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0443\u0440\u044b', u'\u0414\u0435\u0442\u044f\u043c', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0414\u0435\u0442\u044f\u043c', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u0421\u0435\u043c\u0438\u043d\u0430\u0440\u044b', u'\u0421\u043f\u043e\u0440\u0442', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0443\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0443\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043b\u0443\u0431\u044b', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u0424\u0435\u0441\u0442\u0438\u0432\u0430\u043b\u0438', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u0421\u0435\u043c\u0438\u043d\u0430\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0414\u0435\u0442\u044f\u043c', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0421\u0435\u043c\u0438\u043d\u0430\u0440\u044b', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0414\u0435\u0442\u044f\u043c', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0414\u0435\u0442\u044f\u043c', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0414\u0435\u0442\u044f\u043c', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0426\u0438\u0440\u043a', u'\u042d\u043a\u0441\u043a\u0443\u0440\u0441\u0438\u0438', u'\u0414\u0435\u0442\u044f\u043c', u'\u0421\u0435\u043c\u0438\u043d\u0430\u0440\u044b', u'\u0422\u0435\u0430\u0442\u0440\u044b', u'\u0414\u0435\u0442\u044f\u043c'] 

Was ist das? Alles scheint richtig zu sein, und entsprechend der Menge dieser Symbole ist es irgendwie Code des Textes. Was soll ich damit machen?

+1

gesetzt Würden Sie uns den Inhalt der Seite etwas ausmachen, zeigt, bevor Sie es analysieren? – tramstheman

+1

Es ist kyrillisch. Es zeigt sich für mich in PowerShell gut. – TigerhawkT3

+0

@ TigerhawkT3 was soll ich tun, um es normalerweise in erhabenen Editor sehen wir sagen? –

Antwort

-1

Die Daten sind da - Sie sehen die Darstellung von unicode Strings, die auf einem Terminal angezeigt werden, das nur das Rendern von ASCII unterstützt.

>>> print(u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b') 
>>> Концерты 

Hier ist ein Beispiel für die Ausgabe von Python 2 auf meinem System

>>> print u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b' 
Концерты 

ich glaube, das Problem, dass, wenn Sie print ein list es zeigt die repr jedes Element in der Liste:

>>> print [u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b'] 
[u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b'] 
>>> print u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b' 
Концерты 

Wenn Sie jedes Element drucken möchten, dann durchlaufen Sie die Liste und drucken Sie sie direkt:

>>> items = [u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b', u'8 \u043c\u0430\u0440\u0442\u0430', u'\u041a\u043e\u043d\u0446\u0435\u0440\u0442\u044b'] 
>>> for item in items: print item 
... 
Концерты 
Концерты 
8 марта 
Концерты 

Wenn es nicht richtig angezeigt wird, überprüfen, was Ihre stdout Codierung

>>> import sys 
>>> sys.stdout.encoding 
'UTF-8' 
+0

Und wie kann ich in etwas lesbar kodieren (in diesem Fall Text ist cyrrylic) –

+0

@Vinand wo möchten Sie den Text anzeigen? –

+0

Es ist nicht nur das Terminal; Die Codepage hängt auch von der Version von Python ab. Es funktioniert, wie Sie in Python 3 demonstrieren, aber nicht in Python 2, das OP verwendet. – TigerhawkT3

Verwandte Themen