【问题标题】:Fetching the <td> element using Beautiful Soup使用 Beautiful Soup 获取 <td> 元素
【发布时间】:2016-10-30 23:55:50
【问题描述】:

我可以使用漂亮的汤来获取 TD 元素中的文本 &lt; list_selected &gt; 元素

soup.find_all(class_ = {"list_selected"}) 

之后我必须获取 NAME 部分。相似块的数量。

<tr>
        <td align="left" style="padding-left: 3px;padding-right:3px;" class="list_selected">1422</td>
        <td align="left" style="padding-left: 3px;padding-right:3px;" class="data">123456</td>
        <td align="left" style="padding-left: 3px;padding-right:3px;" class="data">NAME</td>
</tr>

【问题讨论】:

    标签: python python-2.7 beautifulsoup


    【解决方案1】:
    soup.find_all("td", { "class" : "list_selected" })
    

    这将为您获取td 节点。结果是根据documentation 的节点列表。

    【讨论】:

      【解决方案2】:

      Beautiful soap 有一个名为 (.text) 的方法来获取 html 文件的内部内容

      我在下面更正了您的代码以获取内部文本

       from bs4 import BeautifulSoup
      
       soup1 = BeautifulSoup('<td align="left" style="padding-left:3px;padding-right:3px;" class="list_selected">1422</td>',"lxml")
      
       second= soup1.find("td", {"class": "list_selected"}) #Finding td class
      
       name = second.text #Getting inner text contents of td class
      
       print name #Displays inner text
      

      希望你一切都正确:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-15
        • 2020-07-04
        相关资源
        最近更新 更多