首页 > 解决方案 > Python BeautifulSoup 通过实时价格循环

问题描述

我正在尝试获取美国不同地点的特定商品的价格。该网站有一个文本框,您可以在其中输入状态并更新价格,但 URL 保持不变。在开发者工具中,位置会根据您更新价格的输入而改变。我正在尝试使用以下代码来获取价格:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.sunbeltrentals.com/equipment/detail/1255/0350140/7500lb-mini-excavator/').text


soup = BeautifulSoup(source, 'lxml')

    # Finding the equipment name and printing it

name_article = soup.find('div', id = 'equipment-catalog-page-container')

    # This pricing is based off of typing "NC" into the search box

equipment_name = name_article.h1.text
print(equipment_name)


for price_article in soup.find_all('div', class_ = 'info--price'):
    equipment_price = price_article.text
    print(equipment_price)

输出是:

7,500lb Mini Excavator

Online Rate: 

1 Day: {{RateDaily | toCurrency}} {{Currency}}
1 Week: {{RateWeekly | toCurrency}} {{Currency}}
4 Weeks: {{RateMonthly | toCurrency}} {{Currency}}

The displayed rates do not include the taxes or optional charges that may be selected later in the checkout process.

作为参考,开发人员工具中用于定价方面的 HTML 代码如下:

   <div class="info--price" v-if="showRates">
<span class="ratesSubtitle">
 Online Rate:
</span>
<br/>
<ul class="menu">
 <li style="margin-right: 10px;">
  1 Day:
  <strong>
   {{RateDaily | toCurrency}}
  </strong>
  {{Currency}}
 </li>
 <li style="margin-right: 10px;">
  1 Week:
  <strong>
   {{RateWeekly | toCurrency}}
  </strong>
  {{Currency}}
 </li>
 <li style="margin-right: 10px;">
  4 Weeks:
  <strong>
   {{RateMonthly | toCurrency}}
  </strong>
  {{Currency}}
 </li>
</ul>
<small>
 The displayed rates do not include the taxes or optional charges that may be selected later in the checkout process.
</small>

在开发人员工具屏幕上,它显示 {{RateMonthly | toCurrency }} 有一个价格(例如:1,125 美元),它写着 {{Currency}} 的地方写着“美元”。我不知道如何让这些在开发人员工具上显示实际价格。

我不确定它是否重要,但这里是该位置的开发人员工具:

<div id="jobsite-instance">
 <jobsite-display inline-template="" v-cloak="">
  <div v-if="showJobsite">
   <div class="jobsite_location" v-show="!showInput">
    <p>
     Jobsite Location
    </p>
    <p style="cursor: pointer;" v- 
 on:click.prevent="showInput=true">
     <span class="location raised">
      {{currentLocation}}
     </span>
      <i aria-hidden="true" class="fa fa-pencil-square-o" 
style="margin-left: 4px;">
     </i>
    </p>
   </div>
   <shopping-location-selector :current- 
location="currentLocation" v-show="showInput">
   </shopping-location-selector>
  </div>
 </jobsite-display>
</div>

在我的屏幕上显示 {{currentLocation}} 的位置显示“NC”。我不确定如何解决这些变量,我做错了什么吗?我的下一步将是创建一个循环来更改搜索位置,以便我可以使用它来获取不同州的价格。

标签: pythonweb-scrapingbeautifulsoup

解决方案


推荐阅读