【发布时间】:2015-11-03 07:09:49
【问题描述】:
有一些用于从 xml 内容解析 dom 树的包,例如 https://docs.python.org/2/library/xml.dom.minidom.html。
但我不想针对 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) 是这样的。
那么有什么方法可以像使用无头浏览器、selenium 那样我可以解析整个 DOM 树结构并通过子和子子来访问目标元素?
【问题讨论】:
标签: python selenium web-scraping phantomjs