当前位置:   article > 正文

爬取穷游网的酒店数据_基于穷游网酒店的数据采集与分析

基于穷游网酒店的数据采集与分析

需要的工具:python3,requests,beautifulSoup,time,re等库。

 

from bs4 import BeautifulSoup
import requests
import time
import re
 
url = 'http://search.qyer.com/hotel/89580_4.html'
urls = ['http://search.qyer.com/hotel/89580_{}.html'.format(str(i)) for i in range(1,10)] # 最多157页
infos = []
print(urls)
 
# 批量爬取数据
def getAUrl(urls):
    data_number = 0
    for url in urls:
        getAttractions(url)
        print('--------------{}-----------------'.format(len(infos)),sep='\n')
        getInfosByPrice()
 
# 爬取当页面数据
def getAttractions(url,data = None):
    web_data = requests.get(url)
    time.sleep(2)
    soup = BeautifulSoup(web_data.text,'lxml')
    #print(soup)
 
    hotel_names = soup.select('ul.shHotelList.clearfix > li > h2 > a')
    hotel_images = soup.select('span[class="pic"] > a > img')
    hotel_points = soup.select('span[class="points"]')
    hotel_introduces = soup.select('p[class="comment"]')
    hotel_prices = soup.select('p[class="seemore"] > span > em')
 
    if data == None:
        for name,image,point,introduce,price in \
                zip(hotel_names,hotel_images,hotel_points,hotel_introduces,hotel_prices):
            data = {
                'name':name.get_text().replace('\r\n','').strip(),
                'image':image.get('src'),
                'point':re.findall(r'-?\d+\.?\d*e?-?\d*?', point.get_text())[0],
                'introduce':introduce.get_text().replace('\r\n','').strip(),
                'price':int(price.get_text())
            }
            #print(data)
            infos.append(data)
        #getInfosByPrice(infos)
 
# 根据价格从高到低进行排序
def getInfosByPrice(infos = infos):
    infos = sorted(infos, key=lambda info: info['price'], reverse=True)
    for info in infos:
        print(info['price'], info['name'])
 
#getAttractions(url)
getAUrl(urls)

运行情况

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/138453
推荐阅读
相关标签
  

闽ICP备14008679号