Python百度云网盘搜索引擎源码及安装教程

本文介绍了如何使用Python搭建一个百度云网盘搜索引擎。首先,你需要PHP 5.3.7+、MySQL和Python 2.7+以及xunsearch搜索引擎。获取源码后,部署包括创建数据库、导入SQL、网站部署(支持nginx和apache)、配置文件修改。接着启动爬虫,运行spider.py进行数据抓取。最后,安装xunsearch并建立索引,使搜索引擎能正常工作。
摘要由CSDN通过智能技术生成

运行环境
开始之前你需要安装

•PHP 5.3.7 +
•MySQL
•Python 2.7 ~
•xunsearch 搜索引擎
获取源码
ssh 方式:

git clone git@github.com:k1995/BaiduyunSpider.git
https 方式:

git clone https://github.com/k1995/BaiduyunSpider
或手动下载

https://github.com/k1995/BaiduyunSpider/archive/master.zip
下载完毕后,项目的目录结构大致是这样的

--- indexer/  #索引
--- spider/   #爬虫
--- sql/     
--- web/      #网站
    --- application/
        --- config/ # 配置相关
                --- config.php
                --- database.php # 数据库配置
                ...
        ...
    --- static/ # 存放静态资源,css|js|font
    --- system/
    --- index.php
    ...
开始部署
创建数据库
创建名为pan的数据库,编码设为utf-8。然后导入sql,完成表的创建。

网站部署
支持nginx,apache 服务器。

apache 需要开启 mod_rewrite 。

nginx  配置如下

location /
{  
    index index.php;
    try_files $uri $uri/ /index.php/$uri;
}

location ~ [^/]\.php(/|$)
{
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
    include pathinfo.conf;
}
配置文件修改
config.php 文件修改网站标题,描述等信息

database.php 修改数据库账号,密码等信息

网站是基于CodeIgniter 框架开发的,如安装,部署,或二次开发有问题,请参考官网文档


启动爬虫
进入 spider/目录,修改spider.py 中数据库信息。

如果你是第一次部署,需运行下面命令,完成做种

python spider.py --seed-user
上面其实就是抓取百度云热门分享用户的相关信息,然后从他们开始入手爬取数据

然后运行

python spider.py
此时爬虫已经开始工作了

安装xunsearch
目前使用xunsearch作为搜索引擎,后面会更换为elasticsearch。

安装过程请参考(不需要安装,PHP SDK,我已经整合到web里了)

http://xunsearch.com/doc/php/guide/start.installation

索引数据
上面我们完成了爬虫的数据抓取,网站的搭建,但还不能搜索,下面开始最后一步,索引的建立。

进入 indexer/目录,在indexer.php中将$prefix,替换为你web的根路径

require '$prefix/application/helpers/xs/lib/XS.php';
并修改数据库账号密码

然后运行

./indexer.php

# -*- coding: utf-8 -*-
import urllib2,re,argparse,json,time
import MySQLdb as mdb
import metautils,traceback,Queue,socket
import random
"""
/*
 *--------------------------------------------   
 *
 *  
 *    
 *  Github 仓库: https://github.com/k1995/BaiduyunSpider
 * 
 *  演示:http://www.11bt.net/ *
 *   
 * ----------------------------------------*/
"""

DB_HOST='127.0.0.1'
DB_PORT='3306'
DB_USER='root'
# MySQL密码
DB_PASS='123123'
# 数据库名称
DB_NAME='pan'
SPIDER_INTERVAL=1

ERR_NO=0#正常
ERR_REFUSE=1#爬虫爬取速度过快,被拒绝
ERR_EX=2#未知错误


proxy_list = [
               {'http':"x.x.x.x:8080"},
               {'http':"x.x.x.x:8081"},
               {'http':"x.x.x.x:8082"},
               {'http':"x.x.x.x:8083"},
               {'http':"x.x.x.x:8084"},
               {'http':"x.x.x.x:8085"},
               {'http':"x.x.x.x:8086"},
               {'http':"x.x.x.x:8087"},
               {'http':"x.x.x.x:8088"},
               {'http':"x.x.x.x:8089"}
                ]




def getHtml(url,ref=None,reget=5):
 try:

  uas = [
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9b4) Gecko/2008030317 Firefox/3.0b4",
    "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; BIDUBrowser 7.6)",
    "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko",
    "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0",
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; Touch; LCJB; rv:11.0) like Gecko",
    ]
  proxy_ip =random.choice(proxy_list)

  ua=random.choice(uas)

  print proxy_ip
  print ua
  proxy_support = urllib2.ProxyHandler(proxy_ip)
  opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
  urllib2.install_opener(opener)

  request = urllib2.Request(url)
  time.sleep(5)
  request.add_header('User-Agent', ua)
  if ref:
   request.add_header('Referer',ref)
  page = urllib2.urlopen(request,timeout=30)
  html = pag
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值