首页 > 解决方案 > Beautifulsoup 对数组中文本的抓取问题

问题描述

数据=

<div class="dojoxGridView" id="dojox_grid__View_1" role="presentation" style="width: 1900px; height: 721px; left: 1px; top: 0px;" widgetid="dojox_grid__View_1">
       <input class="dojoxGridHiddenFocus" dojoattachpoint="hiddenFocusNode" role="presentation" type="checkbox"/>
       <input class="dojoxGridHiddenFocus" role="presentation" type="checkbox"/>
       <div class="dojoxGridScrollbox" dojoattachpoint="scrollboxNode" role="presentation" style="height: 721px;">
        <div class="dojoxGridContent" dojoattachpoint="contentNode" hidefocus="hidefocus" role="presentation" style="height: 504px; width: 1900px;">
         <div role="presentation" style="position: absolute; left: 0px; top: 0px;">
          <div aria-selected="false" class="dojoxGridRow" role="row" style="">
           <table border="0" cellpadding="0" cellspacing="0" class="dojoxGridRowTable" role="presentation" style="width: 1900px;">
            <tbody>
             <tr>
              <td class="dojoxGridCell" idx="0" role="gridcell" style="display:none;width:100px;" tabindex="-1">
               78126
              </td>
              <td class="dojoxGridCell" idx="1" role="gridcell" style="width:10%;" tabindex="-1">
               Approved Plan
              </td>
              <td class="dojoxGridCell" idx="2" role="gridcell" style="width:10%;" tabindex="-1">
               G-10
              </td>
              <td class="dojoxGridCell" idx="3" role="gridcell" style="width:40%;" tabindex="-1">
               ROOF PLAN
              </td>
             </tr>
            </tbody>
           </table>
          </div>

输入=

    source = driver.page_source
soup = BeautifulSoup(source, "lxml")
print(soup. prettify())
for article in soup.find_all('div', class_='dojoxGridContent'):
drawing_no = article.find_all('td', class_='dojoxGridCell', idx='3')
# ->need one more line to extract text
print(""drawing_no")

输出=

<td class="dojoxGridCell" idx="3" role="gridcell" style="width:40%;" tabindex="-1">ROOF PLAN</td> ...

我只想提取“屋顶计划”我应该如何编辑我的代码?我尝试了 drawing_no.text 和 drawing_no.value 但它说“没有属性”。谢谢你的帮助!

标签: python-3.xweb-scrapingbeautifulsoup

解决方案


尝试跟随代码

source="""<div class="dojoxGridView" id="dojox_grid__View_1" role="presentation" style="width: 1900px; height: 721px; left: 1px; top: 0px;" widgetid="dojox_grid__View_1">
       <input class="dojoxGridHiddenFocus" dojoattachpoint="hiddenFocusNode" role="presentation" type="checkbox"/>
       <input class="dojoxGridHiddenFocus" role="presentation" type="checkbox"/>
       <div class="dojoxGridScrollbox" dojoattachpoint="scrollboxNode" role="presentation" style="height: 721px;">
        <div class="dojoxGridContent" dojoattachpoint="contentNode" hidefocus="hidefocus" role="presentation" style="height: 504px; width: 1900px;">
         <div role="presentation" style="position: absolute; left: 0px; top: 0px;">
          <div aria-selected="false" class="dojoxGridRow" role="row" style="">
           <table border="0" cellpadding="0" cellspacing="0" class="dojoxGridRowTable" role="presentation" style="width: 1900px;">
            <tbody>
             <tr>
              <td class="dojoxGridCell" idx="0" role="gridcell" style="display:none;width:100px;" tabindex="-1">
               78126
              </td>
              <td class="dojoxGridCell" idx="1" role="gridcell" style="width:10%;" tabindex="-1">
               Approved Plan
              </td>
              <td class="dojoxGridCell" idx="2" role="gridcell" style="width:10%;" tabindex="-1">
               G-10
              </td>
              <td class="dojoxGridCell" idx="3" role="gridcell" style="width:40%;" tabindex="-1">
               ROOF PLAN
              </td>
             </tr>
            </tbody>
           </table>
          </div>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(source,"html.parser")
for article in soup.find_all('div', class_='dojoxGridContent'):
  drawing_no = article.find('td', class_='dojoxGridCell', idx='3')
  if drawing_no:
    print(drawing_no.get_text())

推荐阅读