python连接mysql,并整理(去哪儿网)页面数据到表

##引入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()

运行结果:

相关推荐
北欧人写代码11 分钟前
MySQL 数据库备份与恢复
mysql
sin220112 分钟前
springboot数据校验报错
spring boot·后端·python
eric-sjq29 分钟前
基于xiaothink对Wanyv-50M模型进行c-eval评估
人工智能·python·语言模型·自然语言处理·github
是十一月末40 分钟前
机器学习之KNN算法预测数据和数据可视化
人工智能·python·算法·机器学习·信息可视化
工业互联网专业43 分钟前
基于OpenCV和Python的人脸识别系统_django
人工智能·python·opencv·django·毕业设计·源码·课程设计
杜小白也想的美1 小时前
FlaskAPI-初识
python·fastapi
一只搬砖的猹1 小时前
cJson系列——常用cJson库函数
linux·前端·javascript·python·物联网·mysql·json
CodeClimb2 小时前
【华为OD-E卷-租车骑绿道 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
CodeClimb2 小时前
【华为OD-E卷-MVP争夺战 100分(python、java、c++、js、c)】
java·python·华为od
冰镇毛衣2 小时前
4.3 数据库HAVING语句
数据库·sql·mysql