有一些從xml內容解析dom樹的軟件包,如https://docs.python.org/2/library/xml.dom.minidom.html。有沒有什麼辦法解析網站內容的DOM樹?
但我不想要目標xml,只有html網站的網頁內容。
from htmldom import htmldom
dom = htmldom.HtmlDom("http://www.yahoo.com").createDom()
# Find all the links present on a page and prints its "href" value
a = dom.find("a")
for link in a:
print(link.attr("href"))
但對於這個我收到此錯誤:
Error while reading url: http://www.yahoo.com
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/htmldom/htmldom.py", line 333, in createDom
raise Exception
Exception
見我已經籤BeautifulSoup,但不是我想要的。 Beautifulsoup僅適用於html頁面。如果頁面內容使用Javascript動態加載,則失敗。我不想分析使用getElementByClassName
和類似的元素。但是dom.children(0).children(1)
這樣的事情。
那麼有沒有什麼辦法像使用無頭瀏覽器,硒我可以解析整個DOM樹結構,並通過子和subchild我可以訪問targget元素?