我試圖讀取並處理網頁在Python具有像在它下面幾行:閱讀網頁與Python
<div class="or_q_tagcloud" id="tag1611"></div></td></tr><tr><td class="or_q_artist"><a title="[Artist916]" href="http://rateyourmusic.com/artist/ac_dc" class="artist">AC/DC</a></td><td class="or_q_album"><a title="[Album374717]" href="http://rateyourmusic.com/release/album/ac_dc/live_f5/" class="album">Live</a></td><td class="or_q_rating" id="rating374717">4.0</td><td class="or_q_ownership" id="ownership374717">CD</td><td class="or_q_tags_td">
我目前只在藝術家的名字感興趣(AC/DC)和專輯名稱(Live)。我可以使用libxml2dom來讀取和打印它們,但我無法弄清楚如何區分鏈接,因爲每個鏈接的節點值都是None。
一個顯而易見的方法是一次讀取輸入行,但有沒有更聰明的方式來處理這個html文件,以便我可以創建兩個單獨的列表,其中每個索引匹配另一個或具有此信息的結構?
import urllib
import sgmllib
import libxml2dom
def collect_text(node):
"A function which collects text inside 'node', returning that text."
s = ""
for child_node in node.childNodes:
if child_node.nodeType == child_node.TEXT_NODE:
s += child_node.nodeValue
else:
s += collect_text(child_node)
return s
f = urllib.urlopen("/home/x/Documents/rym_list.html")
s = f.read()
doc = libxml2dom.parseString(s, html=1)
links = doc.getElementsByTagName("a")
for link in links:
print "--\nNode " , artist.childNodes
if artist.localName == "artist":
print "artist"
print collect_text(artist).encode('utf-8')
f.close()
你能告訴我們你目前的代碼嗎?也許你需要明確引用anchor的firstChild? (文本節點) – 2010-08-09 15:14:21
我沒有看到一次讀取輸入行有什麼問題。 – katrielalex 2010-08-09 15:25:26
只需要注意一下,如果你的for循環可以重複多次:創建新字符串就像地獄一樣昂貴(它們是不可變的 - 你最終每次都會創建一個新對象),並且每次迭代都會執行一次。最好追加到列表中,然後在循環後加上'''.join()'列表。它可以使戲劇性的加速。 – Daenyth 2010-08-09 20:21:11