首页 编程教程正文

【django框架】基础例子2,适合入门学习实践操作

piaodoo 编程教程 2020-02-22 22:08:51 997 0 python教程

本文来源吾爱破解论坛

本帖最后由 huguo002 于 2019-10-30 18:40 编辑

【django框架】基础例子2,适合入门学习实践操作

感兴趣的可以看看第一篇:
django框架例子,非常简单适合参考学习入门!


实现详情页跳转,最新8篇文章跳转,上下文翻页效果!

模型层(models):
新增了id,用于匹配详情页

[Python] 纯文本查看 复制代码

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

[Python] 纯文本查看 复制代码
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[previous_index]
            next_qner = qunaer_list[next_index]
            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
新增了 详情页的匹配

[Python] 纯文本查看 复制代码
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,)
]


最终效果展示:

去哪儿1.jpg (164.6 KB, 下载次数: 1)

下载附件  保存到相册

2019-10-30 18:27 上传


去哪儿2.jpg (71.52 KB, 下载次数: 1)

下载附件  保存到相册

2019-10-30 18:27 上传



如有帮助,请个好评!给个热心!给个赞!

附上项目包:
链接: https://pan.baidu.com/s/1lk2ykKko0jo7oyivpW8GhQ 提取码: pfb6

比较基础,算是一个记录吧!求大牛带飞!
欢迎交流 python!

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/7686.html

搜索