django框架例子,非常简单适合参考学习入门!
django框架例子,非常简单适合参考学习入门!hello word
def hello_world(request):
return HttpResponse("hello world!")
爬取去哪儿景点信息并展示
前面的帖子有分享的爬虫
【爬虫】简单的demo,去哪儿门票相关信息爬取https://www.52pojie.cn/thread-1038403-1-1.html
项目urls:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('qunaer/', include('qunaer.urls')),
]
app urls:
from django.urls import path
from . import views
urlpatterns=[
path('',views.hello_world,),
path('index/',views.index,)
]
models 模型层:
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,不然会报错!
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '项目名称.settings')
django.setup()
app 视图层:
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
page
项目打包,整个项目文件
链接: https://pan.baidu.com/s/1wR8dtq2oD4yEAIY6QA48Lg 提取码: cru6
如果对你有帮助,请给个好评!!
欢迎交流 python! 谢谢分享~~~ 给你点个赞赞赞 过来学习一下 我來學看看
谢谢分享 感谢大大 谢谢分享
页:
[1]