一、加载html
1.编辑scrapyweb/urls.py
from django.conf.urls import url
from webapp.views import index
urlpatterns = [
url(r'^$', index),
]
2.编辑scrapyweb/webapp/views.py
from django.shortcuts import render_to_response
def index(req):
return render_to_response('index.html')
3.编辑scrapyweb/webapp/templates/index.html
input
4.页面展示
二、form前后台交互--页面跳转
一、编辑scrapyweb/urls.py
from django.conf.urls import url
from webapp.views import index ,scrapy
urlpatterns = [
url(r'^$', index),
url(r'^scrapy$', scrapy),
]
2.编辑scrapyweb/webapp/views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
def index(req):
return render_to_response('index.html')
# 接收请求数据
def scrapy(request):
request.encoding='utf-8'
message='你搜索的内容为: '+request.GET['wd']
return HttpResponse(message)
3.编辑scrapyweb/webapp/templates/index.html
input
4.页面展示
二、form前后台交互--页面不跳转
一、编辑scrapyweb/urls.py
from django.conf.urls import url
from webapp.views import index ,do_scrapy_url
urlpatterns = [
url(r'^$', index),
url(r'^do_scrapy_url$', do_scrapy_url),
]
2.编辑scrapyweb/webapp/views.py
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render_to_response
from webapp.scrapyall import start_crawler
def index(req):
#return HttpResponse('hello django')
# url=start_crawler("https://blog.csdn.net/")
return render_to_response('index.html')
# 接收请求数据
def do_scrapy_url(request):
#u = request.POST.get('user', None)
#url = request.POST.get('urlval', None)
ctx = {}
if request.GET:
ctx['url'] = request.GET['urlval']
else:
ctx['url']='没有url'
return render(request,'index.html', ctx )
3.编辑scrapyweb/webapp/templates/index.html
scrapy
{{ url }}
4.页面展示