如何使用Python中的BeautifulSoup按文本查找特定标记

时间:2017-04-26 16:08:24

标签: python selenium-webdriver beautifulsoup selenium-chromedriver

<table class="person show-interviews interviews-loaded" application="43352812" current-interview-stage-id="373822" candidate_hiring_plan="52607">

    <tbody><tr class="basic-info clickable candidate">
      <td class="photo-column" href="/people/34284587?application_id=43352812&amp;src=search">
    <a href="/people/34284587?application_id=43352812&amp;src=search"><img class="person-photo" width="40" height="40" alt="Candidate Profile Picture" src="https://gravatar.com/avatar/b6d305a017cc572d47807d9e6812bef1.png?s=40&amp;d=https%3A%2F%2Fcdn.greenhouse.io%2Fassets%2Fsilhouette-7fdf9a27e7e8acd6f7cad72986479543.png"></a>
</td>

      <td class="person-info-column" href="/people/34284587?application_id=43352812&amp;src=search">
  <p class="name">
      <a href="/people/34284587?application_id=43352812&amp;src=search">Chew Bacca</a>

        <img class="email-candidate-icon" title="Email Chew" width="16" modal_path="/people/34284587/email_candidate_modal?application_id=43352812" src="https://cdn.greenhouse.io/assets/icons/email-fd1e71440bb47a93b13bccdbffa4d311.png" alt="Email">


  </p>

</td>

      <td class="job-info-column" href="/people/34284587?application_id=43352812&amp;src=search">
    <p class="job">Consulting Engineer </p>

      <div class="status">
            <a class="toggle-interviews" href="#">1 interview to schedule for Face to Face</a>
      </div>
</td>

      <td class="interview-kit-column" nofollow="true">
  <div class="interview-kit-wrapper">
      <span class="interview-kit-icon"></span><br>
      <a modal_path="/people/34284587/applications/43352812/submit_feedback_options" class="submit-feedback-link" href="#">interview kit</a>
  </div>
  <label class="bulk-checkbox-wrapper">
    <input class="bulk-checkbox" type="checkbox">
  </label>
</td>

    </tr>

          <tr class="availability">
    <td colspan="3" class="details name">
      <div class="header">
  <div class="left-col">
    <span class="title closed no-expand">Availability</span>
    <span class="state">
      <div class="dropdown">
        <button name="button" type="submit" id="quick_action_304014813" class="link-like-button" data-toggle="dropdown" aria-has-popup="true" aria-expanded="false">Not Requested</button>
        <ul class="dropdown-menu" aria-labelledby="quick_action_304014813">
          <li data-type="state" data-url="/people/availability/304014813/state" data-state="not_requested" class="dropdown-item" data-current-state="true">Not Requested</li>
<li data-type="state" data-url="/people/availability/304014813/state" data-state="requested" class="dropdown-item">Requested</li>
<li data-type="state" data-url="/people/availability/304014813/state" data-state="received" class="dropdown-item">Received</li>
<li data-type="state" data-url="/people/availability/304014813/state" data-state="confirmation_sent" class="dropdown-item">Confirmation Sent</li>
          <li data-type="action" data-url="/people/availability/edit_modal/304014813?force=true" data-action="edit_availability" class="dropdown-item action-item">ENTER AVAILABILITY MANUALLY</li>
<li data-type="action" data-url="/people/availability/cofirm_modal/304014813?force=true" data-action="send_confirmation" class="dropdown-item action-item">SEND INTERVIEW CONFIRMATION</li>
        </ul>
      </div>
      <span class="action-time"></span>
    </span>
  </div>
  <span class="action">
    <button name="button" type="submit" class="link-like-button availability-modal-open" modal_path="/people/availability/request_modal/304014813" data-modal-path="/people/availability/request_modal/304014813">Request Availability</button>
  </span>
</div>
<div class="body">
  <div class="times-container">
    <div class="times proposed">
      <div class="title">Suggested Times:</div>
      <ul>
      </ul>
    </div>
    <div class="times candidate">
      <div class="title">
        Chew is available at these times:
      </div>
        Not yet responded <button name="button" type="button" modal_path="/people/availability/edit_modal/304014813" class="link-like-button availability-edit-modal-open">Edit</button>
    </div>
  </div>
</div>

    </td>
    <td class="interview-kit-column"></td>
  </tr>

  
<tr class="interview spicy" application_id="43352812" step_id="553192" stage_id="" style="">
  <td colspan="2" rowspan="1" class="name" href="/guides/553364/people/34284587?application_id=43352812" title="View Interview Kit">
    <span class="interview-kit-icon small"></span>Cultural Fit Interview
  </td>


      <td class="details">
        <div class="wrapper">
          <div class="interview-info">
            Skipped <span href="/interviews/49710750/unskip" class="unskip-link">Unskip</span>
          </div>
        </div>
      </td>

    <td class="interview-kit-column">
    </td>
</tr>

  
<tr class="interview spicy" application_id="43352812" step_id="553193" stage_id="" style="">
  <td colspan="2" rowspan="1" class="name" href="/guides/553365/people/34284587?application_id=43352812" title="View Interview Kit">
    <span class="interview-kit-icon small"></span>Peer Panel Interview
  </td>


      <td class="details">
        <div class="wrapper">
          <div class="interview-info">
            Skipped <span href="/interviews/49710751/unskip" class="unskip-link">Unskip</span>
          </div>
        </div>
      </td>

    <td class="interview-kit-column">
    </td>
</tr>

  
<tr class="interview spicy" application_id="43352812" step_id="553194" stage_id="" style="">
  <td colspan="2" rowspan="1" class="name" href="/guides/553366/people/34284587?application_id=43352812" title="View Interview Kit">
    <span class="interview-kit-icon small"></span>Case Study
  </td>


      <td class="details">
        <div class="wrapper">
          <div class="interview-info">
            Skipped <span href="/interviews/49710752/unskip" class="unskip-link">Unskip</span>
          </div>
        </div>
      </td>

    <td class="interview-kit-column">
    </td>
</tr>

  
<tr class="interview spicy" application_id="43352812" step_id="553195" stage_id="" style="">
  <td colspan="2" rowspan="1" class="name" href="/guides/553367/people/34284587?application_id=43352812" title="View Interview Kit">
    <span class="interview-kit-icon small"></span>Executive Interview
  </td>


      <td class="details">
        <div class="wrapper">
          <div class="interview-info">
            Skipped <span href="/interviews/49710753/unskip" class="unskip-link">Unskip</span>
          </div>
        </div>
      </td>

    <td class="interview-kit-column">
    </td>
</tr>

  
<tr class="interview spicy" application_id="43352812" step_id="4883928" stage_id="" style="">
  <td colspan="2" rowspan="1" class="name" href="/guides/4884061/people/34284587?application_id=43352812" title="View Interview Kit">
    <span class="interview-kit-icon small"></span>Challenge
  </td>


      <td class="details schedulable removable" modal_path="/interviews/schedule?application_id=43352812&amp;interview_kit_id=4884061" modal_title="Consulting Engineer  (Austin, New York City, Palo Alto)" nofollow="true" title="Schedule Interview">

        <div class="wrapper">
            <span href="/interviews/49710754/skip" class="x" title="Skip this interview"></span>

            <span class="to-be-scheduled-icon"></span>

          <div class="interview-info">
                <a href="/interviews/scheduler?application_id=43352812&amp;interview_kit_id=4884061">Schedule Interview</a>
            <div class="integration-buttons">
</div>

          </div>
        </div>
      </td>


    <td class="interview-kit-column">
    </td>
</tr>

  
<tr class="interview spicy" application_id="43352812" step_id="4883933" stage_id="" style="">
  <td colspan="2" rowspan="1" class="name" href="/guides/4884066/people/34284587?application_id=43352812" title="View Interview Kit">
    <span class="interview-kit-icon small"></span>Personality Assessment
  </td>


      <td class="details">
        <div class="wrapper">
          <div class="interview-info">
            Skipped <span href="/interviews/49710755/unskip" class="unskip-link">Unskip</span>
          </div>
        </div>
      </td>

    <td class="interview-kit-column">
    </td>
</tr>




  </tbody></table>

<table class="person show-interviews interviews-loaded" application="31024648" current-interview-stage-id="373842" candidate_hiring_plan="52610">

    <tbody><tr class="basic-info clickable candidate">
      <td class="photo-column" href="/people/5879170?application_id=31024648&amp;src=search">
    <a href="/people/5879170?application_id=31024648&amp;src=search"><img class="person-photo" width="30" height="40" alt="Candidate Profile Picture" src="https://prod-heroku.s3.amazonaws.com/people/photos/005/879/170/resized/imgres.jpg?AWSAccessKeyId=AKIAIK36UTOKQ5F2YNMQ&amp;Expires=1495711223&amp;Signature=GuPHCM1nw%2B2tC%2F44rHejCRvnsx0%3D"></a>
</td>

      <td class="person-info-column" href="/people/5879170?application_id=31024648&amp;src=search">
  <p class="name">
      <a href="/people/5879170?application_id=31024648&amp;src=search">Jessica Alba</a>


        &nbsp;<span class="alert" title="Jessica Alba has been in Phone Interview for more than 14 days">Alert</span>

  </p>

    <p class="title">New York University</p>
</td>

      <td class="job-info-column" href="/people/5879170?application_id=31024648&amp;src=search">
    <p class="job">Enterprise Account Executive (North America)</p>

      <div class="status">
            <a class="toggle-interviews" href="#">1 interview to schedule for Phone Interview</a>
      </div>
</td>

      <td class="interview-kit-column" nofollow="true">
  <div class="interview-kit-wrapper">
      <span class="interview-kit-icon"></span><br>
      <a modal_path="/people/5879170/applications/31024648/submit_feedback_options" class="submit-feedback-link" href="#">interview kit</a>
  </div>
  <label class="bulk-checkbox-wrapper">
    <input class="bulk-checkbox" type="checkbox">
  </label>
</td>

    </tr>

        

    <tr class="availability">
    <td colspan="3" class="details name">
      <div class="header">
  <div class="left-col">
    <span class="title closed no-expand">Availability</span>
    <span class="state">
      <div class="dropdown">
        <button name="button" type="submit" id="quick_action_210624650" class="link-like-button" data-toggle="dropdown" aria-has-popup="true" aria-expanded="false">Not Requested</button>
        <ul class="dropdown-menu" aria-labelledby="quick_action_210624650">
          <li data-type="state" data-url="/people/availability/210624650/state" data-state="not_requested" class="dropdown-item" data-current-state="true">Not Requested</li>
<li data-type="state" data-url="/people/availability/210624650/state" data-state="requested" class="dropdown-item">Requested</li>
<li data-type="state" data-url="/people/availability/210624650/state" data-state="received" class="dropdown-item">Received</li>
<li data-type="state" data-url="/people/availability/210624650/state" data-state="confirmation_sent" class="dropdown-item">Confirmation Sent</li>
          <li data-type="action" data-url="/people/availability/edit_modal/210624650?force=true" data-action="edit_availability" class="dropdown-item action-item">ENTER AVAILABILITY MANUALLY</li>
<li data-type="action" data-url="/people/availability/cofirm_modal/210624650?force=true" data-action="send_confirmation" class="dropdown-item action-item">SEND INTERVIEW CONFIRMATION</li>
        </ul>
      </div>
      <span class="action-time"></span>
    </span>
  </div>
  <span class="action">
    <button name="button" type="submit" class="link-like-button availability-modal-open" modal_path="/people/availability/request_modal/210624650" data-modal-path="/people/availability/request_modal/210624650">Request Availability</button>
  </span>
</div>
<div class="body">
  <div class="times-container">
    <div class="times proposed">
      <div class="title">Suggested Times:</div>
      <ul>
      </ul>
    </div>
    <div class="times candidate">
      <div class="title">
        Jessica is available at these times:
      </div>
        Not yet responded <button name="button" type="button" modal_path="/people/availability/edit_modal/210624650" class="link-like-button availability-edit-modal-open">Edit</button>
    </div>
  </div>
</div>

    </td>
    <td class="interview-kit-column"></td>
  </tr>

  
<tr class="interview spicy" application_id="31024648" step_id="553218" stage_id="" style="">
  <td colspan="2" rowspan="1" class="name" href="/guides/553390/people/5879170?application_id=31024648" title="View Interview Kit">
    <span class="interview-kit-icon small"></span>Technical Phone Interview
  </td>


      <td class="details schedulable removable" modal_path="/interviews/schedule?application_id=31024648&amp;interview_kit_id=553390" modal_title="Enterprise Account Executive (North America)" nofollow="true" title="Schedule Interview">

        <div class="wrapper">
            <span href="/interviews/23067896/skip" class="x" title="Skip this interview"></span>

            <span class="to-be-scheduled-icon"></span>

          <div class="interview-info">
                <a href="/interviews/scheduler?application_id=31024648&amp;interview_kit_id=553390&amp;return_to=https%3A%2F%2Fapp.greenhouse.io%2Fpeople%3Fsort%3Dlast_activity%2Bdesc%26stage_status_id%255B%255D%3D2%26type%3Dall%26interview_status_id%255B%255D%3D2%26interview_status_id%255B%255D%3D1%26partial%3Dtrue&amp;return_to_label=Back+to+Search+Results">Schedule Interview</a>
            <div class="integration-buttons">
</div>

          </div>
        </div>
      </td>


    <td class="interview-kit-column">
    </td>
</tr>


</tbody></table>

有多个表类(人员show-interview interview-loaded)。我想从文本匹配或包含Challenge的类中提取类。我想忽略其他类。这是我到目前为止所尝试的:

with open('Page_Source.html') as page_source:

        soup=BeautifulSoup(page_source,'html.parser')

        for table in soup.findAll('table',{'class':'person show-interviews interviews-loaded'}):
                name=table.find('p',{'class':'name'}).find('a').text
                #print name
                #print table['application']
                #print table['current-interview-stage-id']
                job_title=table.find('p',{'class':'job'}).text
                #print job_title
                next_interview_details=table.find('a',{'class':'toggle-interviews'}).text
                #print next_interview_details
                
                for tr in table.findAll('tr',{'class':'interview spicy'}):
                        i=tr.find('td',text='Challenge')
                        print i
                

2 个答案:

答案 0 :(得分:1)

您可以应用filtering function来检查所需的表格,以检查表格的“文字”中是否存在Challenge子字符串:

for table in soup.find_all(lambda tag: tag.name == 'table' and 'Challenge' in tag.get_text()):
    print(table.get('class'))

打印:

['person', 'show-interviews', 'interviews-loaded']

答案 1 :(得分:0)

请求BeautifulSoup为您提供表格列表。然后查看每个表格,询问它是否包含“挑战”。如果是,则显示该表的class属性。

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(open('temp.htm').read(),'lxml')
>>> tables = soup.findAll('table')
>>> for table in tables:
...     if 'Challenge' in table.text:
...         table.attrs['class']
...         
['person', 'show-interviews', 'interviews-loaded']

编辑:对评论的回应。我这次没有把代码写成过滤器,因为我想让逻辑变得更明显。

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(open('temp.htm').read(),'lxml')
>>> tables = soup.findAll('table')
>>> for table in tables:
...     '----->', table.attrs['class']
...     target_tds = [_.parent for _ in table.findAll('span', attrs={'class': 'interview-kit-icon small'})]
...     for target_td in target_tds:
...         target_td.text.strip(), 'Skipped' in target_td.fetchNextSiblings()[0].text
... 
('----->', ['person', 'show-interviews', 'interviews-loaded'])
('Cultural Fit Interview', True)
('Peer Panel Interview', True)
('Case Study', True)
('Executive Interview', True)
('Challenge', False)
('Personality Assessment', True)
('----->', ['person', 'show-interviews', 'interviews-loaded'])
('Technical Phone Interview', False)