2016-01-17 58 views
1

我有一個Linux命令從這裏https://github.com/scragg0x/FFXIV-Scraper調用命令,並保留原有格式和數據類型

lodestoner topics 

返回這樣的數據:

/usr/lib/python2.7/site-packages/beautifulsoup4-4.4.1-py2.7.egg/bs4/__init__.py:166: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently. 

To get rid of this warning, change this: 

BeautifulSoup([your markup]) 

to this: 

BeautifulSoup([your markup], "lxml") 

[ 
    { 
     "body": "<div class=\"area_inner_cont\">\n<a href=\"//na.finalfantasyxiv.com/lodestone/topics/detail/2f33323151437bd543ae8637766e0bd3c6d741ac\"><img alt=\"\" height=\"149\" src=\"http://img.finalfantasyxiv.com/t/2f33323151437bd543ae8637766e0bd3c6d741ac.png?1452844930\" width=\"570\"/></a>\n\t\t\t\t\t\tThe date has been set for the twenty-seventh installment of the Letter from the Producer LIVE! Streaming live from <b class=\"text-strong\">Kagoshima</b>, Japan, Producer &amp; Director Yoshi-P will answer questions from players across the globe. Don't miss this chance to get the latest information to come out of Eorzea!<br/>\n<br/>\r\nRead on for <a href=\"//na.finalfantasyxiv.com/lodestone/topics/detail/2f33323151437bd543ae8637766e0bd3c6d741ac\" rel=\"2f33323151437bd543ae8637766e0bd3c6d741ac\">details</a>.\n\t\t\t\t\t</div>", 
     "lang": "en", 
     "title": "Letter from the Producer LIVE Part XXVII", 
     "timestamp": 1452844800, 
     "link": "//na.finalfantasyxiv.com/lodestone/topics/detail/2f33323151437bd543ae8637766e0bd3c6d741ac", 
     "id": "2f33323151437bd543ae8637766e0bd3c6d741ac" 
    }, 

....... 

    "title": "The \u201dLetter from the Producer LIVE Part XXVI\u201d Digest Released!", 

我期待這個腳本在Python並保留格式。我已經做到了這一點:

proc = subprocess.Popen(['lodestoner', 'topics'], stdout=subprocess.PIPE) 
(xml, err) = proc.communicate() 
exit_code = proc.wait() 

但它似乎並沒有保留格式:

b'[\n {\n  "body": "<div class=\\"area_inner_cont\\">\\n<a href=\\"//na.finalfantasyxiv.com/lodestone/topics/detail/2f33323151437bd543ae8637766e0bd3c6d741ac\\"><img alt=\\"\\" height=\\"149\\" src=\\"http://img.finalfantasyxiv.com/t/2f33323151437bd543ae8637766e0bd3c6d741ac.png?1452844930\\" width=\\"570\\"/></a>\\n\\t\\t\\t\\t\\t\\tThe date has been set for the twenty-seventh installment of the Letter from the Producer LIVE! Streaming live from <b class=\\"text-strong\\">Kagoshima</b>, Japan, Producer &amp; Director Yoshi-P will answer questions from players across the globe. Don\'t miss this chance to get the latest information to come out of Eorzea!<br/>\\n<br/>\\r\\nRead on for <a href=\\"//na.finalfantasyxiv.com/lodestone/topics/detail/2f33323151437bd543ae8637766e0bd3c6d741ac\\" rel=\\"2f33323151437bd543ae8637766e0bd3c6d741ac\\">details</a>.\\n\\t\\t\\t\\t\\t</div>", \n  "lang": "en", \n  "title": "Letter from the Producer LIVE Part XXVII", \n  "timestamp": 1452844800, \n  "link": "//na.finalfantasyxiv.com/lodestone/topics/detail/2f33323151437bd543ae8637766e0bd3c6d741ac", \n  "id": "2f33323151437bd543ae8637766e0bd3c6d741ac"\n }]' 

我錯過了什麼?我如何將它帶入Python並正確處理(BeautifulSoup/XML)?例如,如果我想打印title

回答

1

這是一個json字符串(字節串)。使用json.loads解碼後的字節串爲一個字符串對其進行解碼:

(xml, err) = ... 
... 

objects = json.loads(xml.decode()) 
print([o['title'] for o in objects]) 

如果你只想打印XML,解碼xml(字節字符串對象),並打印:

print(xml.decode()) 
+0

我認爲這是正確的道路,但一些符號似乎在絆倒它。它正在打印:'['Producer LIVE Part XXVII的信件','最終幻想XIV的磁盤空間需求變更:重生在所有平臺上的境界',' 結束腳本'等等(有10+項目)。我在該行/符號中編輯了我的OP。 – Zeno

+0

'\ u201d'被unicode絆倒了 – Zeno

+0

@Zeno,''\ u201d'=='「''',怎麼了? – falsetru

相關問題