【问题标题】:Cant get the price (Error list index out of range)无法获取价格(错误列表索引超出范围)
【发布时间】:2019-06-16 10:48:33
【问题描述】:

我必须为我的大学完成一个 WebScraping 项目,我正在从一个房地产网站上抓取,我无法从中得到价格,我需要的所有其他数据都可以正常工作。写错了

列表索引超出范围

但它正是我需要的。

var_4 = first.find_all("span", class_="pull-right")[0]

https://www.willhaben.at/iad/immobilien/mietwohnungen/niederoesterreich/wiener-neustadt?areaId=304&parent_areaid=3&page=1

这是链接,每个价格都有相同的("span", class_="pull-right")

我需要价格数据来分析我的箱线图和图表。

【问题讨论】:

  • 什么是first 变量?
  • 它的空间以 m^2 为单位
  • 您的意思是info 类元素?
  • 我用它来命名进程,var_4 只是我写它的数字方式,比如 var_1 = first.find_all("span", class_="desc-left")[0]。文本,我试图对价格做同样的事情来创建一个数据框,但它不起作用
  • 当您执行first.find_all 时,您会在first 元素中找到。

标签: python-3.x web-scraping


【解决方案1】:

您可以正则表达式价格。我还提取了adId,以便您可以链接到 id。 item['title'] 会给你标题。我将其保留为 dict,以便您可以通过 adId 查找价格。

import requests, re, urllib ,json

r = requests.get('https://www.willhaben.at/iad/immobilien/mietwohnungen/niederoesterreich/wiener-neustadt?areaId=304&parent_areaid=3&page=1')
p1 = re.compile(r'var tmsJson = (.*);')
data = json.loads(urllib.parse.unquote(json.loads(p1.findall(r.text)[0])['tmsData']['search_results']))
listings = dict([(item['adId'], item.get('price', 'N/A')) for item in data])
print(listings)

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多