#!/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
}
分享到:
相关推荐
知乎爬虫和v2ex爬虫的实现。使用python的pyspider爬虫进行开发,主要爬取知乎的问题和评论,以及v2ex的帖子。数据转储到mysql数据库,用于zhihu项目的使用。
该资源为pyspider爬虫开源框架,也可以从github官网下载到
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的...
python网络爬虫pyspider.75, python网络爬虫pyspider.75
python项目
pycurl+pyspider+phabtomjs, pycurl+pyspider+phabtomjs
源码下载 欢迎下载
pyspider中文手册
文章目录一、需求二、分析三、Code准备工作main.pyspider.py 一、需求 使用任意代理 IP 进行如下操作 使用requests模块进行豆瓣电影的个人用户登录操作 使用requests模块访问个人用户的电影排行榜->分类排行榜->任意...
Python爬虫的学习历程-PySpider
【python爬虫】资源pyspider-v0.3.10
Requests+正则表达式爬取猫眼电影 分析Ajax请求并抓取今日头条街拍美图 使用Selenium模拟浏览器抓取淘宝商品美食信息 使用Redis+Flask维护动态代理池 使用代理处理反爬抓取微信文章 使用Redis+Flask维护动态Cookies...
包含如下依赖环境: python-2.7.12.amd64 lxml-2.2.8.win-amd64-py2.7 ez_setup-0.9.tar pip-8.1.2.tar 结合我的blog文章使用: http://blog.csdn.net/zengraoli/article/details/52415139
python3.7安装pyspider修复文件,修复后即可支持3.7,避免冲突。
python3.7+安装pyspider报错解决方案.docx
linux python windows 网页爬虫 网页抓取
pyspider依赖库,需要手动安装,上传的两个python3版本,分别为3.8版本和3.6版本,支持的计算机位数为64位
在本篇内容里小编给大家整理的是一篇关于Python3爬虫中pyspider的安装步骤,需要的朋友们可以学习参考下。
包含如下依赖环境: python-2.7.12.amd64 lxml-2.2.8.win-amd64-py2.7 ez_setup-0.9.tar pip-8.1.2.tar
## 使用python爬虫爬取知乎和v2ex数据,充实网站信息 安装python2.7并且配置环境变量。同时安装pycharm,配置interpretor,安装pip。 这里会各种报错,主要是中文目录以及pip版本导致的错误,需要修改各种配置...