pyspider 爬豆瓣电影信息

本文介绍了一个使用Python实现的豆瓣电影爬虫项目,通过解析网页获取电影详情,包括标题、导演、演员等信息,并存储到MongoDB数据库中。爬虫采用pyspider框架,按不同标签抓取电影列表。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2015-11-23 10:50:38
# Project: doubanmovie
from pyspider.libs.base_handler import *
import pymongo,sys
from pymongo import MongoClient
import re,time,random,hashlib
from scrapy.selector import Selector
from datetime import *
class Handler(BaseHandler):

client = MongoClient(host="localhost",port=27017, read_preference=pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED)
db = client.edogdata
db.authenticate("database","passwd")

@every(minutes=24*60)
def on_start(self):
self.crawl('http://movie.douban.com/tag/', callback=self.index_page) #豆瓣电影标签

@config(age=10 * 24 * 60 * 60)
def index_page(self, response):
for each in response.doc('a[href^="http"]').items():
if re.match("http://www.douban.com/tag/\w+", each.attr.href, re.U): # 爱情,戏剧,刘德华,1990,惊悚,恐怖 。。。
self.crawl(each.attr.href, callback=self.list_page)

@config(age=2, priority=2)
def list_page(self, response):
#print "urlllll response",response.doc
movie = response.doc('a[href^="http"]').items()
for each in movie:
if re.match("http://www.douban.com/link2/\W+",each.attr.href):
#print "each",each.text()
#print "each.attr.href",each.attr.href
if each.attr.href.find("movie") < 130:
self.crawl(each.attr.href, callback=self.final_page)
print "find movie"

def final_page(self,response):
for each in response.doc('a[href^="http"]').items():
if re.match('http://movie.douban.com/\w+',each.attr.href,re.U):
self.crawl(each.attr.href,callback=self.detail_page)
#翻页
self.crawl([x.attr.href for x in response.doc('.next a').items()],callback=self.final_page) # .next 的 '.' 表示class=next ,如果是 #next 的话,则表示 id=next ×××××××××××××××××××××××××××××××××××××××××××××××××××
#@config(priority=4)
def detail_page(self, response):
now = str(datetime.now())
_id = hashlib.md5(response.doc('HTML>BODY>DIV#wrapper>DIV#content>H1>SPAN').text().encode('utf-8')).hexdigest()
site = response.url
name = response.doc("title").text().split('(')[0]#response.doc('HTML>BODY>DIV#wrapper>DIV#content>H1>SPAN[property="v:itemreviewed"]').text()
director = [x.text() for x in response.doc('a[rel="v:directedBy"]').items()]
release_date = [x.text() for x in response.doc('span[property="v:initialReleaseDate"]').items()]
actor = '/'.join([x.text() for x in response.doc('a[rel="v:starring"]').items()])
rating = [x.text() for x in response.doc('strong[property="v:average"]').items()]
type = [x.text() for x in response.doc('span[property="v:genre"]').items()]
source = '豆瓣'
#IMDb_Link = [x.attr.href for x in response.doc('a[rel="nofollow"]').items()]
IMDb_Link = response.doc('HTML>BODY>DIV#wrapper>DIV#content>DIV.grid-16-8.clearfix>DIV.article>DIV.indent.clearfix>DIV.subjectwrap.clearfix>DIV.subject.clearfix>DIV#info>a[rel="nofollow"]').attr.href # HTML>BODY 可以不用
self.db.douban.insert({"_id":_id,"url":site , "title":name,"time":now,"director":director,"release_date": release_date,'actor':actor,"type": type,"source": source,"rating":rating,"IMDb_Link":IMDb_Link})
return {
"url": response.url,
"title": name,#response.doc('HTML>BODY>DIV#wrapper>DIV#content>H1>SPAN[property="v:itemreviewed"]').text(),#property="v:itemreviewed
"rating": rating,#response.doc('HTML>BODY>DIV#wrapper>DIV#content>DIV.grid-16-8.clearfix>DIV.article>DIV.indent.clearfix>DIV.subjectwrap.clearfix>DIV#interest_sectl>DIV.rating_wrap.clearbox>DIV.rating_self.clearfix>STRONG.ll.rating_num').text(),
"导演": actor,#[x.text() for x in response.doc('a[rel="v:directedBy"]').items()],
"time": now,
"release_date" : release_date,
"actor" : actor,
"type" : type,
"IMDb_Link" : IMDb_Link
}
<think>我们正在回答用户关于如何启动pyspider虫的问题。根据引用[1][2][3][5]中的信息pyspider可以通过命令行启动,并且通常需要先创建一个项目,然后编写虫脚本,最后运行虫。启动pyspider通常有两种方式:通过命令行直接启动,或者通过pyspider自带的Web界面进行操作。步骤:1.安装pyspider:确保已经正确安装pyspider。如果安装遇到问题,可以参考引用[4]中的避坑指南。2.创建项目(可选):如果是新项目,可以使用`pyspidercreate`命令创建,但通常我们也可以直接编写脚本。3.编写虫脚本:在项目中编写虫脚本(通常是一个Python文件)。4.启动虫:使用`pyspider`命令启动虫。详细说明:根据引用[1]和[2]:-通过命令行创建一个新的虫项目:`pyspidercreateproject_name`-然后进入项目目录,编写虫脚本。但是,实际上pyspider启动虫的方式是运行`pyspider`命令,然后通过Web界面来管理虫项目(包括创建、编辑、运行等)。也可以直接运行单个虫脚本(使用`pyspiderrun`命令)。引用[5]提到在CentOS上搭建PySpider虫服务,其中安装依赖库后,可以直接运行`pyspider`命令启动。因此,启动pyspider虫的常用方法:1.启动pyspider服务(包括Web界面):在命令行输入:pyspiderall这样会启动pyspider的各个组件(包括调度器、抓取器、Web界面等),然后可以通过Web界面(默认端口5000)来管理虫项目。2.通过Web界面操作:在浏览器中访问:http://localhost:5000然后可以创建项目、编写脚本、运行虫。3.直接运行单个虫脚本(不通过Web界面):也可以使用以下命令直接运行一个虫脚本:pyspiderrun[options]<spider_script>注意:在运行之前,请确保已经正确安装了pyspider。如果安装过程中出现问题(如引用[4]中提到的错误),需要先解决安装问题。另外,引用[3]提供了项目地址,用户可以参考官方文档获取更多信息。总结启动步骤:方法一(推荐使用Web界面):步骤1:在命令行运行`pyspiderall`步骤2:打开浏览器,访问http://localhost:5000步骤3:在Web界面中创建项目、编写虫脚本,然后点击“Run”按钮运行虫。方法二(命令行直接运行):步骤1:编写虫脚本,例如:my_spider.py步骤2:在命令行运行`pyspiderrun-cconfig.jsonmy_spider.py`(其中config.json是配置文件,可选)注意事项:-如果遇到端口冲突,可以使用`--port`参数指定其他端口,例如:`pyspiderall--port5010`-如果需要后台运行,可以使用`nohup`命令(Linux)或将其作为服务运行。根据引用[5],在CentOS系统中,可能需要先安装依赖库。因此,我们给出如下回答:</think>### 启动PySpider虫的方法与指南 PySpider启动主要通过命令行操作,以下是详细步骤: #### 一、基础启动流程 1. **启动PySpider服务** 在终端执行: ```bash pyspider all ``` * 这会同时启动调度器(Scheduler)、抓取器(Fetcher)、处理器(Processor)和结果处理器(Result Worker) * 默认Web UI监听端口:`http://localhost:5000` 2. **访问Web控制台** 浏览器打开 `http://localhost:5000` 可: - 创建/管理虫项目 - 实时监控任务状态 - 查看抓取结果和日志 3. **启动单个虫** 对已有虫项目执行: ```bash pyspider -c config.json [project_name] ``` * `config.json` 为配置文件(可选) * `project_name` 需替换为实际项目名 #### 二、关键配置项 常用启动参数: ```bash pyspider all \ --port 5010 # 修改Web UI端口 --phantomjs-proxy 127.0.0.1:25555 # 设置PhantomJS代理 --queue-maxsize 500 # 调整任务队列容量 --debug # 调试模式 ``` #### 三、生产环境部署 1. **后台运行** 使用 `nohup` 保持服务持续运行: ```bash nohup pyspider all > pyspider.log 2>&1 & ``` 2. **分布式部署** 在多台机器分别启动不同组件: ```bash # 调度器节点 pyspider scheduler --database mysql://user:pass@host/db # 抓取器节点 pyspider fetcher --proc-per-worker 10 # 结果处理器 pyspider result_worker ``` #### 四、常见问题解决 - **端口冲突**: 使用 `--port` 指定新端口,例如 `pyspider all --port 5010` - **依赖缺失**: 确保安装必要库(参考安装文档[^5]): ```bash pip install pyspider phantomjs mysql-connector ``` - **Windows启动错误**: 若遇 `Fatal error in launcher`,尝试: ```bash python -m pyspider all # 使用模块方式启动 ``` #### 五、操作示例 1. 创建新项目: ```bash pyspider create --project my_crawler ``` 2. 编辑脚本 `my_crawler.py` 后,通过Web UI或命令行启动: ```bash pyspider run my_crawler.py ``` > **提示**:建议结合 `crontab` 或 `systemd` 实现开机自启和进程守护,详细配置参考官方文档[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值