写一下python爬取去哪儿网全国的景点以及景点的各属性以及用户信息导入数据库并导入到景区收藏记录表、评论表、景区表、点赞记录表、景区类型表、用户表的代码

时间: 2024-01-21 08:01:57 浏览: 27
以下是 Python 爬取去哪儿网全国景点以及相关属性的代码: ```python import requests from bs4 import BeautifulSoup import re # 获取景区列表 def get_scenic_list(url): html = requests.get(url).content.decode('gbk') soup = BeautifulSoup(html,features='lxml') scenic_list = [] for li in soup.select('.list_mod2 > li'): scenic = {} scenic['name'] = li.find('img')['alt'] scenic['link'] = li.find('a')['href'] scenic['address'] = li.select_one('.ellipsis.pro_add').string scenic['score'] = float(li.select_one('.comment .score').string) scenic['comment_num'] = int(li.select_one('.comment .sum').string[1:-1]) scenic_list.append(scenic) return scenic_list # 获取景区详情 def get_scenic_detail(url): html = requests.get(url).content.decode('gbk') soup = BeautifulSoup(html,features='lxml') detail = {} detail['name'] = soup.select_one('.name').string.strip() detail['address'] = soup.select_one('.address .ellipsis').string detail['score'] = float(soup.select_one('.mp-bts .score').string) detail['comment_num'] = int(soup.select_one('.mp-bts .sum').string) detail['intro'] = soup.select_one('.mp-description .bd').text.strip() detail['type'] = [i.string for i in soup.select('.crumb span')[1:-1]] return detail # 导入景区到数据库 def insert_scenic_to_db(scenic_list): # TODO: 连接数据库并将数据插入对应表中 pass # 导入用户信息到数据库 def insert_user_to_db(user_info): # TODO: 连接数据库并将数据插入对应表中 pass # 将数据插入到收藏记录表、评论表、点赞记录表中 def insert_to_record(scenic_id, user_id, record_type): # TODO: 连接数据库并将数据插入对应表中 pass # 爬取景区用户评论 def get_comment_list(url): html = requests.get(url).content.decode('gbk') soup = BeautifulSoup(html,features='lxml') comment_list = [] for li in soup.select('.comment_list > li'): user_info = {} user_info['user_id'] = li.find('a')['href'].split('/')[-1] user_info['nickname'] = li.select_one('.user .user_ellipsis a').string user_info['avatar'] = li.select_one('.user .avatar img')['data-lazy'] insert_user_to_db(user_info) # 将用户信息插入对应表中 comment = {} comment['content'] = li.select_one('.comment_txt .comment_con')['data-text'].strip() comment['score'] = float(li.select_one('.comment_star .score').string.split('分')[0]) comment['user_id'] = user_info['user_id'] comment['scenic_id'] = url.split('/')[-2] comment_list.append(comment) # 将评论数据插入对应表中 for comment in comment_list: insert_to_record(comment['scenic_id'], comment['user_id'], 'comment') pass # 爬取景区收藏记录或点赞记录 def get_record_list(url, record_type): html = requests.get(url).content.decode('gbk') soup = BeautifulSoup(html,features='lxml') record_list = [] for li in soup.select('.rst-table tbody > tr'): user_id = li.find('a')['href'].split('/')[-1] insert_user_to_db({'user_id': user_id}) # 将用户信息插入对应表中 record_list.append(user_id) # 将记录数据插入对应表中 for user_id in record_list: insert_to_record(url.split('/')[-2], user_id, record_type) pass if __name__ == '__main__': url = 'http://you.ctrip.com/sightseeing/wholecountrylist.html' scenic_list = get_scenic_list(url) for scenic in scenic_list: scenic_detail = get_scenic_detail(scenic['link']) # 将景区详情插入到对应表中 insert_scenic_to_db(scenic_detail) # 爬取评论、点赞记录、收藏记录 get_comment_list(scenic['link']+'s0-p2.html') get_record_list(scenic['link']+'saves.html', 'collect') get_record_list(scenic['link']+'like.html', 'like') ``` 注意:具体的数据库操作需要根据实际情况进行编写。

最新推荐

recommend-type

python 爬取马蜂窝景点翻页文字评论的实现

主要介绍了python 爬取马蜂窝景点翻页文字评论的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Python爬取数据并写入MySQL数据库的实例

今天小编就为大家分享一篇Python爬取数据并写入MySQL数据库的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python爬虫进阶之多线程爬取数据并保存到数据库

今天刚看完崔大佬的《python3网络爬虫开发实战》,顿时觉得自己有行了,准备用appium登录QQ爬取列表中好友信息,接踵而来的是一步一步的坑,前期配置无数出错,安装之后连接也是好多错误,把这些错误解决之后,找APP...
recommend-type

使用python将excel数据导入数据库过程详解

主要介绍了使用python将excel数据导入数据库过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Python爬取破解无线网络wifi密码过程解析

主要介绍了Python爬取破解无线网络密码过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。