【问题标题】:Beautiful Soup 'href' parse美丽的汤'href'解析
【发布时间】:2018-05-08 11:01:45
【问题描述】:

假设我想解析页面中所有 href 中的文本,但我的代码似乎不起作用。 我要解析的html文档中有一些sn-ps这样的代码。

<td class="alt1" id="ID" title="SOMETEXT">
                <div>
                     <a href="THETEXTIWANT.html" id="SOME IDENTIFIER">SOME TEXT.</a>
                </div>
</td>

这是我要运行的代码

from bs4 import BeautifulSoup,
    for td in parse.find_all(class_="alt1"):
        a = str(div.get())
        f.writelines(a + '\n')

【问题讨论】:

  • [tag.text for tag in soup.find_all(href=True)] ?

标签: python beautifulsoup html-parsing


【解决方案1】:

使用find_all(href=True)

for td in parse.find_all(class_="alt1"):
    for href in td.find_all(href=True):
        print(href.text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-18
    • 2014-08-11
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2012-02-05
    相关资源
    最近更新 更多