在重复标签中查找文本

时间:2019-04-29 17:47:42

标签: python-2.7 beautifulsoup

尝试从网页获取span类中的特定文本。我可以得到第一个实例,但是不确定如何迭代来获得所需的实例。

<div class="pricing-base__plan-pricing">
<div class="pricing-base__plan-price pricing-base__plan-price--annual">
<sup class="pricing-base__price-symbol">$</sup>

<span class="pricing-base__price-value">14</span></div>
<div class="pricing-base__plan-price pricing-base__plan-price--monthly">
<sup class="pricing-base__price-symbol">$</sup>
<span class="pricing-base__price-value">18</span>
</div>
<div class="pricing-base__term">
<div class="pricing-base__term-wrapper">
<div class="pricing-base__date">mo*</div>
 </div>

我需要在行中输入“ 18” 18

该数字经常更改,这就是我的代码希望抓取的内容。

1 个答案:

答案 0 :(得分:0)

您可以使用如图所示的类别选择器来检索所有价格的列表,然后索引该列表以获取年度和每月

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('https://www.gotomeeting.com/meeting/pricingc')
soup = bs(r.content, 'lxml')
prices = [item.text for item in soup.select('.pricing-base__price-value')]
monthly = prices[1]
annual = prices[0]

您还可以添加父类:

monthly = soup.select_one('.pricing-base__plan-price--monthly .pricing-base__price-value').text
annual = soup.select_one('.pricing-base__plan-price--annual .pricing-base__price-value').text

示例:

enter image description here