python - 如何在 Beautifulsoup Python 中实现 xpath 替代方案的跟随兄弟轴

标签 python selenium beautifulsoup

我正在尝试使用 Bs4、selenium 和 Python 收集文本,我想使用以下方式获取文本 “Lisa Staprans”:

name = str(profilePageSource.find(class_="hzi-font hzi-Man-Outline").div.get_text().encode("utf-8"))[2:-1]

这是代码:

<div class="profile-about-right">
 <div class="text-bold">
  SF Peninsula Interior Design Firm
  <br/>
  Best of Houzz 2015
 </div>
 <br/>
 <div class="page-tags" style="display:none">
  page_type: pro_plus_profile
 </div>
 <div class="pro-info-horizontal-list text-m text-dt-s">
  <div class="info-list-label">
   <i class="hzi-font hzi-Ruler">
   </i>
   <div class="info-list-text">
    <span class="hide" itemscope="" itemtype="http://data-vocabulary.org/Breadcr
umb">
     <a href="http://www.houzz.com/professionals/c/Menlo-Park--CA" itemprop="url
">
      <span itemprop="title">
       Professionals
      </span>
     </a>
    </span>
    <span itemprop="child" itemscope="" itemtype="http://data-vocabulary.org/Bre
adcrumb">
     <a href="http://www.houzz.com/professionals/interior-designer/c/Menlo-Park-
-CA" itemprop="url">
      <span itemprop="title">
       Interior Designers &amp; Decorators
      </span>
     </a>
    </span>
   </div>
  </div>
  <div class="info-list-label">
   <i class="hzi-font hzi-Man-Outline">
   </i>
   <div class="info-list-text">
    <b>
     Contact
    </b>
    : Lisa Staprans
   </div>
  </div>
 </div>
</div>

请告诉我会怎样。

最佳答案

我假设您正在使用 Beautifulsoup,因为您正在使用 class_ 属性字典 - 如果有一个 div 类名为 hzi-font hzi-Man-Outline 则尝试-

str(profilePageSource.find(class_="hzi-font hzi-Man-Outline").findNext('div').get_text().split(":")[-1]).strip()

摘录“Lisa Staprans”

这里 findNext 导航到下一个 div 并提取文本。

关于python - 如何在 Beautifulsoup Python 中实现 xpath 替代方案的跟随兄弟轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33826600/

相关文章:

python - Python 2 和 Python 3 中 sys.path 中的导入顺序和路径

python - 如何在 Django 中创建模型包

Python - Sympy 最小值和最大值

Python Selenium : TypeError: list indices must be integers or slices, 不是 WebElement

java - 切换到 Java 8 后 Webdriver 断言失败

python - 弃用警告 : AppURLopener style of invoking requests is deprecated

java - XPATH:排除具有特定子元素的元素

python - 如何将字符串转换为 BeautifulSoup 对象?

python - Beautiful Soup - 抓取表格特定元素的更好方法

python - 如何着手识别 HTML 树中语义相关但结构不相关的节点