吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3369|回复: 6
收起左侧

[Python 转载] django框架例子,非常简单适合参考学习入门!

[复制链接]
huguo002 发表于 2019-10-29 20:59
django框架例子,非常简单适合参考学习入门!

hello word

[Python] 纯文本查看 复制代码
def hello_world(request):
    return HttpResponse("hello world!")


hello_world.jpg

爬取去哪儿景点信息并展示

前面的帖子有分享的爬虫

[Python] 【爬虫】简单的demo,去哪儿门票相关信息爬取  https://www.52pojie.cn/thread-1038403-1-1.html


项目urls:
[Python] 纯文本查看 复制代码
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('qunaer/', include('qunaer.urls')),
]


app urls:
[Asm] 纯文本查看 复制代码
from django.urls import path
from . import views

urlpatterns=[
    path('',views.hello_world,),
    path('index/',views.index,)
]


models 模型层:
[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="景点地址")

    def __str__(self):
        return self.name



切记在django运行调试py,一定要加载django,不然会报错!

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '项目名称.settings')
django.setup()



app 视图层:

[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,
         })


# 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 = 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()



超级管理员账号/密码:admin/123456

最终效果:

index
index.jpg

page
page.jpg

项目打包,整个项目文件
链接: https://pan.baidu.com/s/1wR8dtq2oD4yEAIY6QA48Lg 提取码: cru6


如果对你有帮助,请给个好评!!

欢迎交流 python!

免费评分

参与人数 4吾爱币 +4 热心值 +3 收起 理由
linridong + 1 + 1 谢谢@Thanks!
我叫周奔跑 + 1 + 1 用心讨论,共获提升!
tohyueyun + 1 热心回复!
枫叶y + 1 + 1 用心讨论,共获提升!

查看全部评分

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

na72131 发表于 2019-10-29 22:59
谢谢分享~~~
tohyueyun 发表于 2019-10-29 23:27
随梦期初 发表于 2019-10-30 08:24
psx1lin 发表于 2019-10-30 08:25
我來學看看
谢谢分享
阿秉 发表于 2019-10-30 08:28
感谢大大
lucka 发表于 2019-10-30 08:53
谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

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

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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