beautifulsoup只选择某些值

时间:2015-09-22 18:41:17

标签: python python-2.7 web-scraping beautifulsoup

以下是网页源代码的一部分。

   <tr>
    <td>
      <a href="/docdollars/doctors/pid/36602">
        <h6>Jane</h6>
      </a>
         Allopathic & Osteopathic Physicians/Internal Medicine
    </td>
    <td>
      <p>NY Medical Ctr<br>New York City, 
      <a href="/docdollars/states/NY">NY</a>
      </p>
    </td>                
  </tr>
  <tr>
    <td>
      <a href="/docdollars/doctors/pid/1091514">
        <h6>Greg</h6>
      </a>
         Allopathic & Osteopathic Physicians/Family Medicine
    </td>
    <td>
      <p>57950 NYC<br>New York City, 
      <a href="/docdollars/states/NY">NY</a>
      </p>
    </td>
  </tr>

我希望抓取的数据看起来像这样:

Jane, Allopathic & Osteopathic Physicians/Internal Medicine, NY Medical Ctr, New York City, NY 
Greg, Allopathic & Osteopathic Physicians/Family Medicine, 57950 NYC, New York City, NY

我的代码(如下)正在部分工作(请参阅下面的评论)。

for i in item.find_all('tr'):
    print i.find('a').find('h6').text  #working fine
    print i.find('td').next_sibling.next_sibling.find('p').text.strip()  # this needs revision
    print i.find('td').text.strip()  # this needs revision

提前感谢您的建议!

1 个答案:

答案 0 :(得分:1)

专注于使用CSS selector找到<h6>元素,然后从那里找到随附的信息:

for header in soup.select('tr td a h6'):
    name = header.get_text(strip=True)
    practice = header.parent.find_next_sibling(text=True).strip()
    address = header.find_parent('td').find_next_sibling('td').get_text(' ', strip=True)
    print name, practice, address

这样就可以找到h6包装器中包含的所有<tr><td><a>个元素。从那里,我们可以返回到父级(<a>链接)并找到下一段文本,并找到父级<td>元素以查找包含该文本的下一个<td>剩下的文字。

在名为soup的变量中输入样本,产生:

>>> for header in soup.select('tr td a h6'):
...     name = header.get_text(strip=True)
...     practice = header.parent.find_next_sibling(text=True).strip()
...     address = header.find_parent('td').find_next_sibling('td').get_text(' ', strip=True)
...     print name, practice, address
... 
Jane Allopathic & Osteopathic Physicians/Internal Medicine NY Medical Ctr New York City, NY
Greg Allopathic & Osteopathic Physicians/Family Medicine 57950 NYC New York City, NY