如何使用Python从html文件中打印特定标签?

时间:2014-03-25 22:12:16

标签: python html python-2.7 python-3.x tags

如何使用Python在this html文件中打印所有<a>标签?

2 个答案:

答案 0 :(得分:3)

使用beautifulsoup库。

from bs4 import BeautifulSoup

with open('text.html') as input_file:
    soup = BeautifulSoup(input_file)
    a_links = soup.find_all('a')
    for link in a_links:
        print link

答案 1 :(得分:0)

使用reBeautiful Soup。根据您是否想要实际包含标签,其中任何一个都应该能够帮助您满足需要。