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()

运行结果:

相关推荐
Python大数据分析@8 分钟前
python操作CSV和excel,如何来做?
开发语言·python·excel
黑叶白树10 分钟前
简单的签到程序 python笔记
笔记·python
Shy96041823 分钟前
Bert完形填空
python·深度学习·bert
上海_彭彭34 分钟前
【提效工具开发】Python功能模块执行和 SQL 执行 需求整理
开发语言·python·sql·测试工具·element
stars_User36 分钟前
MySQL数据库面试题(下)
数据库·mysql
zhongcx011 小时前
使用Python查找大文件的实用脚本
python
Yaml41 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理
yanwushu1 小时前
Xserver v1.4.2发布,支持自动重载 nginx 配置
mysql·nginx·php·个人开发·composer
蓝眸少年CY2 小时前
MySQL 【流程控制】函数
mysql
yyfhq2 小时前
sdnet
python