一、加载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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>input</title>
<style >
.s_url{width:523px;height:32px;line-height:22px;margin:5px 0 0 7px;padding:0;-webkit-appearance:none}
.s_sb{width:95px;height:36px;padding:0;background-color:#ddd;background-position:0 -48px;border:0;cursor:pointer}
</style>
</head>
<body>
<form >
<span >
<input id="kw" name="wd" class="s_url" value="输入url" maxlength="255" autocomplete="off">
</span>
<span >
<input type="submit" id="su" value="爬取" class="s_sb">
</span>
</form>
</body>
</html>
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>input</title>
<style >
.s_url{width:523px;height:32px;line-height:22px;margin:5px 0 0 7px;padding:0;-webkit-appearance:none}
.s_sb{width:95px;height:36px;padding:0;background-color:#ddd;background-position:0 -48px;border:0;cursor:pointer}
</style>
</head>
<body>
<form action="scrapy" method="get"> //注意这里的设置
<span >
<input id="kw" name="wd" class="s_url" value="输入url" maxlength="255" autocomplete="off">
</span>
<span >
<input type="submit" id="su" value="爬取" class="s_sb">
</span>
</form>
</body>
</html>
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>scrapy</title>
<style >
.s_url{width:523px;height:32px;line-height:22px;margin:5px 0 0 7px;padding:0;-webkit-appearance:none}
.s_sb{width:95px;height:36px;padding:0;background-color:#ddd;background-position:0 -48px;border:0;cursor:pointer}
</style>
</head>
<body >
<form action="do_scrapy_url" method="get">
{% csrf_token %}
<input name="urlval" class="s_url" placeholder="请输入url" maxlength="255" autocomplete="off">
<input type="submit" value="爬取" class="s_sb" >
</form>
<p>{{ url }}</p>
</body>
</html>
4.页面展示