基于Spark的共享单车数据存储系统的设计与实现_flask+spider
- 开发语言:Python
- 框架:Flask
- Python版本:python3.8
- 数据库:mysql 5.7
- 数据库工具:Navicat12
- 开发软件:PyCharm
系统展示
系统登录
管理员功能界面
共享单车管理
系统管理
看板界面
摘要
共享单车数据存储系统是一个专门设计用于管理和存储共享单车服务中产生的大量数据的高效系统。该系统能够处理和存储用户骑行数据、车辆分布信息、维护调度记录等关键运营数据。通过采用高容量数据库和优化的数据索引机制,系统确保了数据读写的高速性和稳定性。同时,系统支持快速数据分析功能,帮助运营商洞察使用趋势,优化车辆配置和提升服务质量。此外,系统还具备良好的扩展性,能够适应不断增长的数据量和业务需求。
系统从研究目的、研究现状、开发技术、系统分析、系统设计以及系统功能实现和系统测试等方面进行阐述。本系统主要是针对共享单车数据存储信息的管理,采用Python语言编写和Django框架,数据库使用MySQL数据库。本系统主要包括系统首页,共享单车管理,系统管理,我的信息等功能。经过测试,本系统可以满足共享单车数据存储的基本要求。
研究背景
随着共享经济的兴起,共享单车作为一种新兴的交通方式迅速普及。它不仅解决了“最后一公里”的出行问题,还有助于缓解城市交通压力。然而,随着用户数量和单车规模的快速增长,如何高效地管理和存储海量的共享单车数据成为了一个挑战。这些数据包括用户的骑行记录、车辆的位置信息、使用状态等,对于保障服务的可靠性、提升用户体验以及进行城市交通规划都至关重要。因此,研发一个专门针对共享单车的数据存储系统显得尤为迫切。
关键技术
Python是解释型的脚本语言,在运行过程中,把程序转换为字节码和机器语言,说明性语言的程序在运行之前不必进行编译,而是一个专用的解释器,当被执行时,它都会被翻译,与之对应的还有编译性语言。
同时,这也是一种用于电脑编程的跨平台语言,这是一门将编译、交互和面向对象相结合的脚本语言(script language)。
Flask是一个使用Python编写的轻量级Web应用框架。它被称为一个“微框架”(microframework),因为它只提供Web应用所需的最核心的功能,如路由、会话管理和模板引擎等,而不像一些更全面的框架那样包含数据库层、表单处理等功能。然而,Flask的扩展生态系统非常丰富,开发者可以通过添加扩展来为Flask应用添加这些额外的功能。
Vue是一款流行的开源JavaScript框架,用于构建用户界面和单页面应用程序。Vue的核心库只关注视图层,易于上手并且可以与其他库或现有项目轻松整合。
MYSQL数据库运行速度快,安全性能也很高,而且对使用的平台没有任何的限制,所以被广泛应运到系统的开发中。MySQL是一个开源和多线程的关系管理数据库系统,MySQL是开放源代码的数据库,具有跨平台性。
B/S(浏览器/服务器)结构是目前主流的网络化的结构模式,它能够把系统核心功能集中在服务器上面,可以帮助系统开发人员简化操作,便于维护和使用。
系统分析
对系统的可行性分析以及对所有功能需求进行详细的分析,来查看该系统是否具有开发的可能。
系统设计
功能模块设计和数据库设计这两部分内容都有专门的表格和图片表示。
系统实现
在登录流程中,用户首先在Vue前端界面输入用户名和密码。这些信息通过HTTP请求发送到Python后端。后端接收请求,通过与MySQL数据库交互验证用户凭证。如果认证成功,后端会返回给前端,允许用户访问系统。这个过程涵盖了从用户输入到系统验证和响应的全过程。管理员进入主页面,主要包括对系统首页,共享单车管理,系统管理,我的信息等功能进行操作。
代码实现
class GongxiangdancheSpider(scrapy.Spider): name = 'gongxiangdancheSpider' spiderUrl = 'https://www.maigoo.com/goomai/179538.html?ajax=1&tabnum=05&defaultids=597%2C155147%2C206329&page={}&action=djarticlelist' start_urls = spiderUrl.split(";") protocol = '' hostname = '' realtime = False def __init__(self,realtime=False,*args, **kwargs): super().__init__(*args, **kwargs) self.realtime = realtime=='true' def start_requests(self): plat = platform.system().lower() if not self.realtime and (plat == 'linux' or plat == 'windows'): connect = self.db_connect() cursor = connect.cursor() if self.table_exists(cursor, '86rqdts9_gongxiangdanche') == 1: cursor.close() connect.close() self.temp_data() return pageNum = 1 + 1 for url in self.start_urls: for page in range(1, pageNum): next_link = url.format(page) yield scrapy.Request( url=next_link, callback=self.parse ) # 列表解析 def parse(self, response): _url = urlparse(self.spiderUrl) self.protocol = _url.scheme self.hostname = _url.netloc plat = platform.system().lower() if not self.realtime and (plat == 'linux' or plat == 'windows'): connect = self.db_connect() cursor = connect.cursor() if self.table_exists(cursor, '86rqdts9_gongxiangdanche') == 1: cursor.close() connect.close() self.temp_data() return list = response.css('ul[class="itembox hasttl"] li') for item in list: fields = GongxiangdancheItem() try: fields["biaoti"] = str( self.remove_html(item.css('a[class="title font20 c333 b dhidden"]::text').extract_first())) except: pass try: fields["fengmian"] = str( self.remove_html(item.css('div[class="img "] a img::attr(src)').extract_first())) except: pass try: fields["bianqian"] = str( self.remove_html(item.css('div[class="other font13"] a::text').extract_first())) except: pass try: fields["liulanliang"] = str( self.remove_html(item.css('div[class="attention ccc"]').extract_first())) if '万' in fields["liulanliang"]: fields["liulanliang"] = float(fields["liulanliang"].replace("万", "")) * 10000 except: pass detailUrlRule = item.css('a[class="title font20 c333 b dhidden"]::attr(href)').extract_first() yield scrapy.Request(url=detailUrlRule, meta={'fields': fields}, callback=self.detail_parse, dont_filter=True) # 详情解析 def detail_parse(self, response): fields = response.meta['fields'] itid = str( self.remove_html(response.css('''div.dianzhan a i::attr(data-itid)''').extract_first())) xiajiang = str( self.remove_html(response.css('''div[class="xiajiang c999"] a i::attr(data-itid)''').extract_first())) oUrl = "https://www.maigoo.com/ajaxstream/praise/?itids="+str(itid)+","+str(xiajiang)+",&action=addmorepraisenum" detail_res = requests.get(oUrl) detail_json = json.loads(detail_res.text) fields["dianzanshu"] = detail_json.get(itid) fields["fanduishu"] = detail_json.get(xiajiang) try: fields["zuozhe"] = str( self.remove_html(response.css('''div[class="articlehead "] div[class="qzoneinfo c999"] a.color1::text,div.headinfo div[class="qzoneinfo c999"] span.c666::text''').extract_first())) except: pass try: fields["zhaiyao"] = str( self.remove_html(response.css('''div.content_introduction_text p::text,div.description::text''').extract_first())) except: pass try: fields["detail"] = str( self.remove_html(response.css('''div.mod_body,div#t_container,div.articlecont''').extract_first())) except: pass return fields # 去除多余html标签 def remove_html(self, html): if html == None: return '' pattern = re.compile(r'<[^>]+>', re.S) return pattern.sub('', html).strip() # 数据库连接 def db_connect(self): type = self.settings.get('TYPE', 'mysql') host = self.settings.get('HOST', 'localhost') port = int(self.settings.get('PORT', 3306)) user = self.settings.get('USER', 'root') password = self.settings.get('PASSWORD', '123456') try: database = self.databaseName except: database = self.settings.get('DATABASE', '') if type == 'mysql': connect = pymysql.connect(host=host, port=port, db=database, user=user, passwd=password, charset='utf8') else: connect = pymssql.connect(host=host, user=user, password=password, database=database) return connect # 断表是否存在 def table_exists(self, cursor, table_name): cursor.execute("show tables;") tables = [cursor.fetchall()] table_list = re.findall('(\'.*?\')',str(tables)) table_list = [re.sub("'",'',each) for each in table_list] if table_name in table_list: return 1 else: return 0 # 数据缓存源 def temp_data(self): connect = self.db_connect() cursor = connect.cursor() sql = ''' insert into `gongxiangdanche`( id ,biaoti ,fengmian ,bianqian ,zuozhe ,liulanliang ,zhaiyao ,dianzanshu ,fanduishu ,detail ) select id ,biaoti ,fengmian ,bianqian ,zuozhe ,liulanliang ,zhaiyao ,dianzanshu ,fanduishu ,detail from `86rqdts9_gongxiangdanche` where(not exists (select id ,biaoti ,fengmian ,bianqian ,zuozhe ,liulanliang ,zhaiyao ,dianzanshu ,fanduishu ,detail from `gongxiangdanche` where `gongxiangdanche`.id=`86rqdts9_gongxiangdanche`.id )) ''' cursor.execute(sql) connect.commit() connect.close()系统测试
系统测试是系统开发过程的最后阶段,而且是非常重要的一个阶段,测试阶段繁琐又漫长,很多时候我们都意识不到它的重要性。它的必要性体现在它是能够保障系统质量与可靠性的重要一关,是对系统的最后一步审查。
通过测试以确定用户对该系统的需求是否已经得到了满足,发现问题以后,我们要不断的调试以找出出现问题的具体原因与位置,然后对其进行修改,直到达到本系统要求的正确度。
结论
1.系统性的分析了共享单车数据存储系统的研究背景和国内外的研究现状,简单性的介绍了该系统的理论意义和实践意义;
2.简单介绍了实现的相关技术,包括:Python技术、MySQL数据库、B/S模式等技术;
3.从用户需求方面对系统的功能需求进行了分析;
4.对系统的功能做了详细的设计,并加以实现;
5.对系统做了大量的测试,并对存在的问题加以修改完善。
