【问题标题】:How to scrape the lowest price of hotel of a given area from kayak.com?如何从 kayak.com 获取指定区域的最低酒店价格?
【发布时间】:2016-11-11 22:58:17
【问题描述】:

所以我必须从这个元搜索引擎中搜索最少的酒店。但无法做到这一点。我得到的只是空列表,而我正在查找带有类的元素。尽管请求正在获取我想要的正确 html。我不知道该怎么办? 这是我的代码:

    # -*- coding: utf-8 -*-
"""
Created on Sat Jul 09 13:30:55 2016

@author: sroy
"""

import requests
from bs4 import BeautifulSoup

url = "https://www.kayak.co.in/hotels/Kolkata,India-c44834/2016-07-09/2016-07-10/2guests"
headers = {
'Accept':"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
'Accept-Encoding':"gzip, deflate, sdch, br",
'Accept-Language':"en-US,en;q=0.8",
'Cache-Control':"max-age=0",
'Connection':"keep-alive",
'DNT':1,
'Host':"www.kayak.co.in",
'Referer':"https://www.kayak.co.in/hotels",
'Upgrade-Insecure-Requests':1,
'User-Agent':"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
}

req = requests.get(url, headers=headers)
soup = BeautifulSoup(req.text.encode('utf-8'))


hotel_name = soup.find_all(".title")
price_elems = soup.find_all(".price")

for hotel in hotel_name:
    i=0
    print hotel_name[i]
    print price_elems[i]
    i+=1

它什么也没打印。不知道为什么。有什么问题

【问题讨论】:

标签: python python-2.7 web-scraping request beautifulsoup


【解决方案1】:

您正在使用CSS selectors,但将它们传递给find_all() 方法而不是select()

hotel_name = soup.select(".title")
price_elems = soup.select(".price")

不过,我仍然认为您需要一个真正的浏览器,因为它是一个非常动态的网站。无论如何,请务必阅读使用条款并遵守法律规定。

【讨论】:

  • 此处为客户端模板 span>
    这里是客户端模板
    这里是客户端模板
  • 这是我得到的输出
  • @catch_me 是的,这就是为什么我在答案中有这个注释:)
  • 网站中有一些ajax调用。我想我必须去拿那个。但是找不到
猜你喜欢
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多