使用beautifulsoup,python

时间:2017-06-23 23:12:30

标签: python-3.x beautifulsoup request web-crawler

我正在抓取观光和活动页面,以便获得该页面中提到的价格。在下面的块中提到了价格:

<div class="price-info" data-origin-price="1200" data-lowest-price="1200.0"  data-origin-ccy="JPY" data-discount-percentage="60">
      <span class="before-discount-row">
        <span class="before-discount">25.12</span>
        <span class="currency">EUR</span>
      </span>
    <span class="price-row">
      <span class="price-prefix">From</span>
      <span class="price">10.05</span>
      <span class="currency">EUR</span>
    </span>

我能够在日元(1200)得到价格。在下一步中,我还希望以欧元回收价格。特别是以下子块中的价格:

<span class="price-row">
  <span class="price-prefix">From</span>
  <span class="price">10.05</span>
  <span class="currency">EUR</span>
</span>

但不知何故,我遇到了墙壁。这是我的代码:

import requests
from bs4 import BeautifulSoup
import sys
import urllib

user_agent = {'User-agent': 'Chrome/43.0.2357.124'}

RegionID = "tokyo"

r = requests.get("https://www.govoyagin.com/things_to_do/japan/" +  str(RegionID) + "?page=0" + str(page))
soup = BeautifulSoup(r.content)

g_data = soup.find_all("li", {"class": "activity-list"})
for item in g_data:
      prices = item.find_all("div", {"class": "price-info"})
      for t in price:
          Price_final = item.find_all("span", {"class": "price"})
          print(Price_final)

这是我得到的输出而不是10.05欧元

[<span class="price"></span>]

有人可以帮帮我吗?有没有办法让数字超出范围?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

我想你忘记了最后一个循环:

g_data = soup.find_all("li", {"class": "activity-list"})
for item in g_data:
      prices = item.find_all("div", {"class": "price-info"})
      for t in prices:
          final_prices = item.find_all("span", {"class": "price"})
          for p in final_prices:
              print(p)