##引入requests/pymysql模块
本地安装mysql数据库,安装图形化工具navicat
python
import requests
from pymysql import Connect
#创建客户端连接信息
client = Connect(
host='127.0.0.1',
port=3306,
user='root',
password='',
)
#创建游标
cursor = client.cursor()
cursor.execute('create database if not exists qunar')
cursor.execute('use qunar')
cursor.execute('create table if not exists qunar_hotel('
'id int primary key auto_increment,'
'name char(100),'
'city_name char(30),'
'price int,'
'score int,'
'type char(60),'
'address char(255),'
'comment char(60))')
#引入城市列表
city_url_list = [{
"name": "北京",
"url": "beijing_city"
}, {
"name": "成都",
"url": "chengdu"
}, {
"name": "广州",
"url": "guangzhou"
}, {
"name": "杭州",
"url": "hangzhou"
}, {
"name": "昆明",
"url": "kunming"
}, {
"name": "青岛",
"url": "qingdao"
}, {
"name": "上海",
"url": "shanghai_city"
}, {
"name": "深圳",
"url": "shenzhen"
}, {
"name": "西安",
"url": "xian"
}, {
"name": "重庆",
"url": "chongqing_city"
}, {
"name": "北海",
"url": "beihai"
}, {
"name": "大理",
"url": "dali"
}, {
"name": "大连",
"url": "dalian"
}, {
"name": "东莞",
"url": "dongguan"
}, {
"name": "佛山",
"url": "foshan"
}, {
"name": "福州",
"url": "fuzhou_fujian"
}, {
"name": "贵阳",
"url": "guiyang"
}, {
"name": "桂林",
"url": "guilin"
}, {
"name": "哈尔滨",
"url": "haerbin"
}, {
"name": "海口",
"url": "haikou"
}, {
"name": "合肥",
"url": "hefei"
}, {
"name": "惠州",
"url": "huizhou_guangdong"
}, {
"name": "济南",
"url": "jinan"
}, {
"name": "嘉兴",
"url": "jiaxing"
}, {
"name": "兰州",
"url": "lanzhou"
}, {
"name": "丽江",
"url": "lijiang"
}, {
"name": "南昌",
"url": "nanchang"
}, {
"name": "南京",
"url": "nanjing"
}, {
"name": "南宁",
"url": "nanning"
}, {
"name": "宁波",
"url": "ningbo"
}, {
"name": "黔东南",
"url": "qiandongnan"
}, {
"name": "秦皇岛",
"url": "qinhuangdao"
}, {
"name": "泉州",
"url": "quanzhou"
}, {
"name": "三亚",
"url": "sanya"
}, {
"name": "厦门",
"url": "xiamen"
}, {
"name": "沈阳",
"url": "shenyang"
}, {
"name": "石家庄",
"url": "shijiazhuang"
}, {
"name": "苏州",
"url": "suzhou_jiangsu"
}, {
"name": "太原",
"url": "taiyuan"
}, {
"name": "天津",
"url": "tianjin_city"
}, {
"name": "威海",
"url": "weihai"
}, {
"name": "温州",
"url": "wenzhou"
}, {
"name": "乌鲁木齐",
"url": "wulumuqi"
}, {
"name": "无锡",
"url": "wuxi"
}, {
"name": "武汉",
"url": "wuhan"
}, {
"name": "西宁",
"url": "xining"
}, {
"name": "烟台",
"url": "yantai"
}, {
"name": "长沙",
"url": "changsha"
}, {
"name": "郑州",
"url": "zhengzhou"
}, {
"name": "珠海",
"url": "zhuhai"
}]
for city_dict in city_url_list[:10]:
url = f'https://hotel.qunar.com/napi/hotHotelList?size=150&city={city_dict["url"]}'
headers = {
'user-agent':'https://hotel.qunar.com/napi/hotHotelList?size=150&city={city_dict["url"]}'
}
res = requests.get(url,headers=headers)
res
Data = tuple(map(lambda x:(x.get('name'),
x.get('cityName'),
eval(x.get('price')),
eval(x.get('score')),
x.get('hotelCategory') if x.get('hotelCategory') else 'null',
x.get('locationInfo'),
x.get('commentDesc') if x.get('commentDesc') else 'null',
),res.json()))
insert_sql = 'insert into qunar_hotel(name,city_name,price,score,type,address,comment) values(%s,%s,%s,%s,%s,%s,%s)'
cursor.executemany(insert_sql,Data)
# 提交事务
client.commit()
else:
cursor.close()
client.close()
运行结果: