赞
踩
需要的工具: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)
运行情况
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。