吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2136|回复: 1
收起左侧

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

  [复制链接]
huguo002 发表于 2019-10-30 18:28
本帖最后由 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
去哪儿2.jpg

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

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

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

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
waltzofjack + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ly765893958 发表于 2019-10-30 20:19
沙发啊    感谢分享  必须支持!!!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-16 15:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表