2 Star 7 Fork 1

sent/python基于Selenium框架爬取美团酒店数据

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MAIN.py 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
sent 提交于 6年前 . python代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import getromid,time,json,re
from flask import request,Flask
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
app=Flask(__name__)
@app.route('/getHotel',methods = ['GET'])
def return_hotel():
hotelList=[]
cityname=request.args.get("citynane")
hotelname=request.args.get("hotelname")
#得到时间数组
startTime_for_unix=time.strptime(request.args.get("startTime"), "%Y-%m-%d")
endTime_for_unix=time.strptime(request.args.get("endTime"), "%Y-%m-%d")
#获取酒店id
HotelId=getromid.getroomid(driver,cityname,hotelname)
time.sleep(0.5)
if HotelId==0:
return "0"
#拼接url
HotelUrl="https://i.meituan.com/awp/h5/hotel/poi/deal.html?poiId=%s&startTime=%d&endTime=%d"%(HotelId,int(time.mktime(startTime_for_unix))*1000,int(time.mktime(endTime_for_unix)*1000))
driver.get(HotelUrl)
time.sleep(0.5)
try:
driver.find_element_by_xpath('//*[@id="main"]/section/section[7]/div[2]').click()
except:
pass
time.sleep(1)
temp=0
for it in driver.find_elements_by_xpath('//*[@id="main"]/section/section[7]/ul/li'):
datlist=it.text.split('\n')
datdict={"roomname":datlist[temp],"breakfast":datlist[temp+1]}
temp=temp+2
if re.sub(r'\D',"",datlist[temp])=='' or '点' in datlist[temp]:
temp=temp+1
datdict["Price"]=int(re.sub(r'\D',"",datlist[temp]))
temp=temp+1
else:
datdict["Price"]=int(re.sub(r'\D',"",datlist[temp]))
temp=temp+1
if '券减' in datlist[temp]:
datdict["Originalprice"]=datdict["Price"]+int(re.sub(r'\D',"",datlist[temp]))
print(datdict["Originalprice"])
temp=temp+1
if "仅剩" in datlist[temp]:
temp=temp+1
datdict['hasroom']=datlist[temp]
temp=0
hotelList.append(datdict)
return json.dumps(hotelList,ensure_ascii=False)
if __name__=="__main__":
app.run(host="127.0.0.1",port=8089)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/the_sent/selenium.git
git@gitee.com:the_sent/selenium.git
the_sent
selenium
python基于Selenium框架爬取美团酒店数据
master

搜索帮助