【django框架】基础例子2,适合入门学习实践操作
本帖最后由 huguo002 于 2019-10-30 18:40 编辑【django框架】基础例子2,适合入门学习实践操作
感兴趣的可以看看第一篇:
django框架例子,非常简单适合参考学习入门!
实现详情页跳转,最新8篇文章跳转,上下文翻页效果!
模型层(models):
新增了id,用于匹配详情页
from django.db import models
# Create your models here.
class Qner(models.Model):
name=models.CharField(max_length=20,verbose_name="景点名字")
address=models.CharField(max_length=60,verbose_name="景点地址")
price=models.CharField(max_length=10,verbose_name="景点价格")
qner_id=models.AutoField(primary_key=True)
def __str__(self):
return self.name
视图层(views):
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'qunaer_spider.settings')
django.setup()
from django.shortcuts import render
from django.http import HttpResponse
from qunaer.models import Qner
from django.core.paginator import Paginator
# Create your views here.
def hello_world(request):
return HttpResponse("hello world!")
def index(request):
qunaer_list=Qner.objects.all()
paginator = Paginator(qunaer_list, 15)
page = request.GET.get('page')
page_obj = paginator.get_page(page)
return render(request, 'qunaer/index.html',{
'page_obj': page_obj,
'paginator': paginator,
'is_paginated': True,
})
def get_page(request,qunaer_id):
qunaer_list = Qner.objects.all()
qner=None
previous_index=0
next_index=0
previous_qner=None
next_qner=None
for index,qunaer in enumerate(qunaer_list):
if index == 0:
previous_index = index
next_index = index + 1
elif index==len(qunaer_list)-1:
previous_index = index-1
next_index = index
else:
previous_index = index - 1
next_index = index + 1
if qunaer.qner_id==qunaer_id:
qner=qunaer
previous_qner = qunaer_list
next_qner = qunaer_list
break
top8_qunaer_list=Qner.objects.order_by('-qner_id')[:8]
return render(request, 'qunaer/page.html', {
'qner': qner,
'top8_qunaer_list':top8_qunaer_list,
'next_qner':next_qner,
'previous_qner':previous_qner,
})
# Create your views here.
from fake_useragent import UserAgent
import requests,time
from bs4 import BeautifulSoup
class Qnaer(object):
def __init__(self,city):
self.ua = UserAgent()
self.headers = {"User-Agent": self.ua.random}
self.url = 'https://piao.qunar.com/ticket/list.htm?keyword='
self.city = city
self.pagemax = 10
#self.pagemax = int()
self.hrefs = []
self.data = list()
def get_pagemax(self):
url = f'{self.url}{self.city}'
response = requests.get(url, headers=self.headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
a = soup.find('div', class_="pager").find_all('a')
pagemax = a[-2].get_text()
self.pagemax = int(pagemax)
def get_urllist(self):
for i in range(1, self.pagemax + 1):
url = f'{self.url}{self.city}&page={i}'
print(url)
response = requests.get(url, headers=self.headers)
time.sleep(2)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
divs = soup.find_all('div', class_="sight_item_detail clrfix")
for div in divs:
detail = dict()
name = div.find('a', class_="name").get_text()
detail['name']=name
address = div.find('p', class_="address color999").find('span').get_text()
detail['address']=address
try:
price = div.find('span', class_="sight_item_price").find('em').get_text()
detail['price']=price
except:
price="价格不详!"
detail['price']=price
self.data.append(detail)
href = div.find('h3', class_='sight_item_caption').find('a')['href']
href = f'https://piao.qunar.com{href}'
self.hrefs.append(href)
print(self.hrefs)
time.sleep(5)
def save_data_to_model(self):
for item in self.data:
new_item = Qner()
new_item.name = item['name']
new_item.address = item['address']
new_item.price = item['price']
new_item.save()
if __name__ == "__main__":
spider=Qnaer("北京")
#spider.get_pagemax()
spider.get_urllist()
spider.save_data_to_model()
#qunaer_list =Qner.objects.all().delete() 清空数据
app.urls
新增了 详情页的匹配
from django.urls import path
from . import views
urlpatterns=[
path('',views.hello_world,),
path('index/',views.index,),
path('index/<int:qunaer_id>',views.get_page,)
]
最终效果展示:
如有帮助,请个好评!给个热心!给个赞!
附上项目包:
链接: https://pan.baidu.com/s/1lk2ykKko0jo7oyivpW8GhQ 提取码: pfb6
比较基础,算是一个记录吧!求大牛带飞!
欢迎交流 python! 沙发啊{:301_986:}感谢分享必须支持!!!
页:
[1]