3
A
回答
8
from lxml.html import parse
dom = parse('http://www.google.com/').getroot()
links = dom.cssselect('a')
+1
很好的答案,只需要做一個'pip install cssselect'來解決問題。 – taystack
1
from lxml import etree, cssselect, html
with open("/you/path/index.html", "r") as f:
fileread = f.read()
dochtml = html.fromstring(fileread)
select = cssselect.CSSSelector("a")
links = [ el.get('href') for el in select(dochtml) ]
links = iter(links)
for n, l in enumerate(links):
print n, l
+1
請注意,cssselect現在是一個獨立的項目,不再使用lxml。用'pip install cssselect'安裝。去[這裏](https://pythonhosted.org/cssselect/)瞭解更多信息。 – jheyse
相關問題
- 1. 使用LXML獲取所有HTML元素
- 2. 如何從使用watir的HTML獲取所有鏈接?
- 3. 使用lxml獲取div的HTML內容
- 4. 使用lxml查找div內的所有鏈接
- 5. 使用lxml從html獲取文本
- 6. 使用左外連接獲取表中的所有鏈接
- 7. 無法使用cURL獲取html鏈接
- 8. HtmlAgilityPack獲取DIV中的所有鏈接
- 9. 獲取頁面上的所有鏈接
- 10. 獲取DOM中的所有href鏈接
- 11. jQuery - 獲取text = mytext的所有鏈接
- 12. NSRegularExpression獲取html內容中的所有下載鏈接
- 13. HTML敏捷包 - 獲取某個類的所有鏈接
- 14. 獲取HTML網頁上的所有鏈接
- 15. 我如何使用easyhtmlparser從python的html文件中獲取所有鏈接?
- 16. 從html獲得所有鏈接,甚至顯示更多鏈接
- 17. lxml - 查找具有某些擴展名的所有鏈接
- 18. 我想獲取所有指向圖片的鏈接,使用jQuery
- 19. 如何獲取所有鏈接,並使用webdriver的
- 20. 如何使用perl獲取網頁中的所有鏈接?
- 21. 使用相同的URL獲取所有Facebook鏈接
- 22. 使用python獲取avalibale在網站中的所有鏈接?
- 23. 如何使用Python獲取域中的所有鏈接?
- 24. 使用Ruby獲取網頁的所有鏈接
- 25. 如何從DOMDocument獲取所有HTML鏈接?
- 26. 無法正確獲取所有鏈接
- 27. 如何獲取所有軟件鏈接?
- 28. 如何獲取所有鏈接標題?
- 29. Jsoup從頁面獲取所有鏈接
- 30. 需要使用webdriver獲取所有可用鏈接
注意,HTML不是XML;如果由於缺少末尾元素或缺少屬性值引號而導致解析有問題,[美麗的湯](http://www.crummy.com/software/BeautifulSoup/)可以幫助或者可能更適合。 –