基于Django+链家+Bootstrap真实数据的房源推荐/可视化系统
最近到了做畢設的時間了,很多小伙伴都選擇了房源推薦/可視化系統,那么我就用Python的Django給大家做一個吧!
一、實現思路及相關技術
所用到的技術棧:
- Python---->Django
- Bootstrap
- 爬蟲
- Mysql
- Html
- Css
- Javascript
二、框架介紹
Django是高水準的Python編程語言驅動的一個開源模型.視圖,控制器風格的Web應用程序框架,它起源于開源社區。使用這種架構,程序員可以方便、快捷地創建高品質、易維護、數據庫驅動的應用程序。這也正是OpenStack的Horizon組件采用這種架構進行設計的主要原因。另外,在Dj ango框架中,還包含許多功能強大的第三方插件,使得Django具有較強的可擴展性?[2]??。Django 項目源自一個在線新聞 Web 站點,于 2005 年以開源的形式被釋放出來。Django 框架的核心組件有:
用于創建模型的對象關系映射;
為最終用戶設計較好的管理界面;
URL 設計;
設計者友好的模板語言;
緩存系統。
django logo
Django(發音:[`d???ɡ??]) 是用python語言寫的開源web開發框架(open source web framework),它鼓勵快速開發,并遵循MVC設計。Django遵守BSD版權,初次發布于2005年7月, 并于2008年9月發布了第一個正式版本1.0 。
Django 根據比利時的爵士音樂家Django Reinhardt命名,他是一個吉普賽人,主要以演奏吉它為主,還演奏過小提琴等。
由于Django在近年來的迅速發展,應用越來越廣泛,被著名IT開發雜志SD Times評選為2013 SD Times 100,位列“API、庫和框架”分類第6位,被認為是該領域的佼佼者?[3]??。
三、系統整體功能框架?
?四、詳細實現
(一)數據獲取
1、獲取租房房源信息
很抱歉,由于爬蟲部分涉及具體網站造成侵權行為,故刪除此部分代碼
?(二)數據庫
加上Django數據遷移的表,一共這些表
?數據庫連接
用pymysql連接本地mysql數據庫,首先通過pip安裝pymysql并創建好數據庫以及數據庫表,導入pymysql包,打開數據庫連接,使用cursor()方法創建一個游標對象,使用execute()方法執行SQL查詢
(三)登陸流程
?
?實現代碼:
登陸:
def login(request):if request.session.get('is_login', None):return redirect('/index')if request.method == "POST":login_form = UserForm(request.POST)message = "請檢查填寫的內容!"if login_form.is_valid():username = login_form.cleaned_data['username']password = login_form.cleaned_data['password']try:user = models.userinfo.objects.get(name=username)print(user.name)if user.password == hash_code(password):request.session['is_login'] = Truerequest.session['user_id'] = user.idrequest.session['user_name'] = user.namereturn redirect('/')else:message = "密碼不正確!"except:message = "用戶不存在!"return render(request, 'login.html', locals())login_form = UserForm()return render(request, 'login.html', locals())注冊:
def register(request):if request.session.get('is_login', None):# 登錄狀態不允許注冊。return redirect("/")if request.method == "POST":register_form = RegisterForm(request.POST)message = "請檢查填寫的內容!"if register_form.is_valid(): # 獲取數據username = register_form.cleaned_data['username']password1 = register_form.cleaned_data['password1']password2 = register_form.cleaned_data['password2']email = register_form.cleaned_data['email']sex = register_form.cleaned_data['sex']if password1 != password2: # 判斷兩次密碼是否相同message = "兩次輸入的密碼不同!"return render(request, 'register.html', locals())elif len(password1)<6:message = "密碼必須大于6位!"else:same_name_user = models.userinfo.objects.filter(name=username)if same_name_user: # 用戶名唯一message = '用戶已經存在,請重新選擇用戶名!'return render(request, 'register.html', locals())same_email_user = models.userinfo.objects.filter(email=email)if same_email_user: # 郵箱地址唯一message = '該郵箱地址已被注冊,請使用別的郵箱!'return render(request, 'register.html', locals())# 創建新用戶new_user = models.userinfo.objects.create()new_user.name = usernamenew_user.password = hash_code(password1)new_user.email = emailnew_user.sex = sexnew_user.save()new_collection = models.Collection.objects.create()new_collection.name = usernamenew_collection.count = 0new_collection.save()return redirect('/login/') # 自動跳轉到登錄頁面register_form = RegisterForm()return render(request, 'register.html', locals())(四)首頁功能
?(五)個人中心
?
?數據分析代碼:
@csrf_exempt def chart(request):district_item = set(models.zufang.objects.values_list('district'))district_item = [i[0] for i in district_item]if request.method == "GET":selected = district_item[0]area_item = set(models.zufang.objects.values_list('area').filter(district=selected))area_item = [i[0] for i in area_item]else:show_chart = Falsearea_item = set(models.zufang.objects.values_list('area').filter(district=request.POST.get('options1')))area_item = [i[0] for i in area_item]print(request.POST.get('options1'))print(request.POST.get('options2'))zufang_totol = models.zufang.objects.values().filter(district=request.POST.get('options1'),area=request.POST.get('options2'))print(zufang_totol.count())if zufang_totol.count() > 0:zheng_list = []he_list = []show_chart = Truefor i in zufang_totol:if i['form'] == "整租":j = list([int(i['price'].strip('-')), int(i['size'].strip('平米'))])zheng_list.append(j)else:j = list([int(i['price'].strip('-')), int(i['size'].strip('平米'))])he_list.append(j)return JsonResponse({"area_item": area_item, "zheng_list": zheng_list, "he_list": he_list, "show_chart": show_chart},safe=False)else:show_chart = Falsereturn JsonResponse({"area_item": area_item, "show_chart": show_chart}, safe=False)return render(request, 'chart.html', locals())?價格預測代碼:
@csrf_exempt def price(request):if request.method == "GET":contact_list = models.zufang.objects.values_list().order_by('id')contact_list = [i for i in contact_list]new_contact_list = random.sample(contact_list, 3)user_name = models.Collection.objects.get(name=request.session.get('user_name'))if user_name.count > 0:collection = user_name.collectioncollection = collection.split(",")collection = [int(i) for i in collection]find_index = []for item in collection:find_index.append(models.zufang.objects.values_list().filter(id=item)[0])find_index = [i for i in find_index]else:collection = []find_index = []if request.GET.get("update_item") == "ok":return JsonResponse({"new_contact_list": new_contact_list, "find_index": find_index}, safe=False)return render(request, 'price.html', locals())else:user_name = models.Collection.objects.get(name=request.session.get('user_name'))count = user_name.countif user_name.count > 0:collection = user_name.collectioncollection = collection.split(",")else:collection = []if request.POST.get("new") == "true":count += 1new_collection = request.POST.get("data_id")collection.append(new_collection)collection = ','.join(collection)new = models.Collection.objects.get(name=request.session.get('user_name'))new.collection = collectionnew.count = countnew.save()else:count -= 1new_collection = request.POST.get("data_id")collection.remove(new_collection)collection = ','.join(collection)new = models.Collection.objects.get(name=request.session.get('user_name'))new.collection = collectionnew.count = countnew.save()return JsonResponse({}, safe=False)總結
項目不足:目前并未實時爬取數據存入數據庫,導致數據更新不及時,有能力的朋友可以自己爬取數據存儲
---------------------------------------------------------------------------------------------------------------------------------
2022年4月25日 更新:
?1)新增鏈家官網爬蟲,可以將最新的數據實時存儲到數據庫
?
?
2)之前版本主頁全是一張圖作為房源的縮略圖,現因為數據庫增加了picture和link字段,故每個房源均為不同的縮略圖且能跳轉到鏈家官網
?3)根據用戶收藏行為,新增協同推薦算法
?(4)更新預測模型,正確填寫信息之后,將會使用預測模型預測出來價格
(5)由于數據庫的數據結構改變,故發布房源增加圖片url和跳轉url輸入框
綜上。?
?
總結
以上是生活随笔為你收集整理的基于Django+链家+Bootstrap真实数据的房源推荐/可视化系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LBP特征提取(C++实现)
- 下一篇: 2016年读书总结(一)