谷鸽或百度的镜像:
1、E:\django下建个文件夹名为搜索引擎→ PyCharm新建项目选Django→location改为 E:\django \搜索引擎→More Settings的Application写个search
***************分割线 ***************
2、项目文件夹搜索引擎:
①settings.py,只改语言和时区
② urls.py:
from django.conf.urls import url
from django.contrib import admin
from search import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index),
url(r'^s', views.so), #搜索网址尾是?及各参数,不加/和$;百度是s,谷鸽是search
url(r'^search', views.so),
]
***************分割线***************
3、应用文件夹search:
①新建俩文件夹templates和static: templates里存放 复制自谷鸽首页的 googleIndex.html,和 强子院子写的 baidu Index.html; static放 images\so.gif等强子所做的百度前端样式。
******分割线******
②views.py:
from django.shortcuts import render
from django.http import HttpResponse
import requests
from fake_useragent import UserAgent
def copySearchEngineData(word):
h = {'User-Agent': UserAgent().random}
#static文件夹,若是 和manage.py同级,则前无/;若用的是在某个应用内建的,则加/
#html=requests.get('https://www.baidu.com %s' %word,headers=h).text
#html=html.replace('//www.baidu.com/img/baidu_jgylogo3.gif',' / static/images/so.gif')
s = requests.session()
javascript=s.get('https://xs5.rqiao.net', headers=h).text.split('cookie\' : "')[1].split('"')[0]
s .cookies['verynginx_sign_javascript']=javascript
html = s.get('https://xs5.rqiao.net%s' %word, headers=h).text
oldLogo='/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png'
html=html.replace(oldLogo,'/static/images/so.gif')
return html
def index(request):
return render(request,'googleIndex.html') #baiduIndex.html
def so(request):
#word=request .GET['wd'] #取输入框的值:text框的name属性的值,百度是wd,Google是q
word=request .get_full_path() #取网址中不含域名的后半截
result=copySearchEngineData(word)
return HttpResponse(result) #展示的是html文件用render,网页源代码则用HttpResponse
******分割线******
③baiduIndex.html:
{% load static from staticfiles %}
<!doctype html>
<html>
<head><meta charset="UTF-8"><title>百度镜像</title></head>
<body>
<p style="text-align:center">
<strong><span style="color:white;font-size:36px;">更纯净,无广告!</span>
<span style="font-size:18px;color:green">绿色的网络搜索环境</span></strong>
</p>
<div style="margin:auto;text-align:center;line-height:360px">
<form action="/s" method="get">
<input type="text" name="wd" style="width:500px;height:40px">
<input type="submit" value="百度一下" style="height:45px">
</form>
</div>
</body>
</html>