基于python实现的抓取腾讯视频所有电影的爬虫


Posted in Python onApril 22, 2016

我搜集了国内10几个电影网站的数据,里面近几十W条记录,用文本没法存,mongodb学习成本非常低,安装、下载、运行起来不会花你5分钟时间。

# -*- coding: utf-8 -*-
# by awakenjoys. my site: www.dianying.at
import re
import urllib2
from bs4 import BeautifulSoup
import string, time
import pymongo
 
NUM  = 0   #全局变量,电影数量
m_type = u''  #全局变量,电影类型
m_site = u'qq' #全局变量,电影网站
 
#根据指定的URL获取网页内容
def gethtml(url):
 req = urllib2.Request(url) 
 response = urllib2.urlopen(req) 
 html = response.read()
 return html
 
#从电影分类列表页面获取电影分类
def gettags(html):
 global m_type
 soup = BeautifulSoup(html)  #过滤出分类内容
 #print soup
 #<ul class="clearfix _group" gname="mi_type" gtype="1">
 tags_all = soup.find_all('ul', {'class' : 'clearfix _group' , 'gname' : 'mi_type'})
 #print len(tags_all), tags_all
 #print str(tags_all[1]).replace('\n', '')
 
 #<a _hot="tag.sub" class="_gtag _hotkey" href="http://v.qq.com/list/1_0_-1_-1_1_0_0_20_0_-1_0.html" title="动作" tvalue="0">动作</a>
 re_tags = r'<a _hot=\"tag\.sub\" class=\"_gtag _hotkey\" href=\"(.+?)\" title=\"(.+?)\" tvalue=\"(.+?)\">.+?</a>'
 p = re.compile(re_tags, re.DOTALL)
 
 tags = p.findall(str(tags_all[0]))
 if tags:
  tags_url = {}
  #print tags
  for tag in tags:
   tag_url = tag[0].decode('utf-8')
   #print tag_url
   m_type = tag[1].decode('utf-8')
   tags_url[m_type] = tag_url 
    
 else:
   print "Not Find"
 return tags_url
 
#获取每个分类的页数
def get_pages(tag_url):
 tag_html = gethtml(tag_url)
 #div class="paginator
 soup = BeautifulSoup(tag_html)  #过滤出标记页面的html
 #print soup
 #<div class="mod_pagenav" id="pager">
 div_page = soup.find_all('div', {'class' : 'mod_pagenav', 'id' : 'pager'})
 #print div_page #len(div_page), div_page[0]
 
 #<a class="c_txt6" href="http://v.qq.com/list/1_2_-1_-1_1_0_24_20_0_-1_0.html" title="25"><span>25</span></a>
 re_pages = r'<a class=.+?><span>(.+?)</span></a>'
 p = re.compile(re_pages, re.DOTALL)
 pages = p.findall(str(div_page[0]))
 #print pages
 if len(pages) > 1:
  return pages[-2]
 else:
  return 1
  
 
def getmovielist(html):
 soup = BeautifulSoup(html)
 
 #<ul class="mod_list_pic_130">
 divs = soup.find_all('ul', {'class' : 'mod_list_pic_130'})
 #print divs
 for div_html in divs:
  div_html = str(div_html).replace('\n', '')
  #print div_html
  getmovie(div_html)
 
 
def getmovie(html):
 global NUM
 global m_type
 global m_site
 
 #<h6 class="caption"> <a href="http://www.tudou.com/albumcover/Z7eF_40EL4I.html" target="_blank" title="徒步旅行队">徒步旅行队</a> </h6> <ul class="info"> <li class="desc">法国卖座喜剧片</li> <li class="cast"> </li> </ul> </div> <div class="ext ext_last"> <div class="ext_txt"> <h3 class="ext_title">徒步旅行队</h3> <div class="ext_info"> <span class="ext_area">地区: 法国</span> <span class="ext_cast">导演: </span> <span class="ext_date">年代: 2009</span> <span class="ext_type">类型: 喜剧</span> </div> <p class="ext_intro">理查德·达奇拥有一家小的旅游公司,主要经营法国游客到非洲大草原的旅游服务。六个法国游客决定参加理查德·达奇组织的到非洲的一...</p>
 
 re_movie = r'<li><a class=\"mod_poster_130\" href=\"(.+?)\" target=\"_blank\" title=\"(.+?)\"><img.+?</li>'
 p = re.compile(re_movie, re.DOTALL)
 movies = p.findall(html)
 if movies:
  conn = pymongo.Connection('localhost', 27017)
  movie_db = conn.dianying
  playlinks = movie_db.playlinks
  #print movies
  for movie in movies:
   #print movie
   NUM += 1
   print "%s : %d" % ("=" * 70, NUM)
   values = dict(
    movie_title = movie[1],
    movie_url = movie[0],
    movie_site  = m_site,
    movie_type  = m_type
    )
   print values
   playlinks.insert(values)
   print "_" * 70
   NUM += 1
   print "%s : %d" % ("=" * 70, NUM)
 
 #else:
 # print "Not Find"
 
def getmovieinfo(url):
 html = gethtml(url)
 soup = BeautifulSoup(html)
 
 #pack pack_album album_cover
 divs = soup.find_all('div', {'class' : 'pack pack_album album_cover'})
 #print divs[0]
 
 #<a href="http://www.tudou.com/albumplay/9NyofXc_lHI/32JqhiKJykI.html" target="new" title="《血滴子》独家纪录片" wl="1"> </a> 
 re_info = r'<a href=\"(.+?)\" target=\"new\" title=\"(.+?)\" wl=\".+?\"> </a>'
 p_info = re.compile(re_info, re.DOTALL)
 m_info = p_info.findall(str(divs[0]))
 if m_info:
  return m_info
 else:
  print "Not find movie info"
 
 return m_info
 
 
def insertdb(movieinfo):
 global conn
 movie_db = conn.dianying_at
 movies = movie_db.movies
 movies.insert(movieinfo)
 
if __name__ == "__main__":
 global conn
 
 tags_url = "http://v.qq.com/list/1_-1_-1_-1_1_0_0_20_0_-1_0.html"
 #print tags_url
 tags_html = gethtml(tags_url)
 #print tags_html
 tag_urls = gettags(tags_html)
 #print tag_urls
 
 
 for url in tag_urls.items():
  print str(url[1]).encode('utf-8') #,url[0]
  maxpage = int(get_pages(str(url[1]).encode('utf-8')))
  print maxpage
 
  for x in range(0, maxpage):
   #http://v.qq.com/list/1_0_-1_-1_1_0_0_20_0_-1_0.html
   m_url = str(url[1]).replace('0_20_0_-1_0.html', '')
   movie_url = "%s%d_20_0_-1_0.html" % (m_url, x)
   print movie_url
   movie_html = gethtml(movie_url.encode('utf-8'))
   #print movie_html
   getmovielist(movie_html)
   time.sleep(0.1)
Python 相关文章推荐
Django中使用locals()函数的技巧
Jul 16 Python
Python实现的rsa加密算法详解
Jan 24 Python
在python中使用requests 模拟浏览器发送请求数据的方法
Dec 26 Python
浅谈pandas筛选出表中满足另一个表所有条件的数据方法
Feb 08 Python
Python文件打开方式实例详解【a、a+、r+、w+区别】
Mar 30 Python
使用python 对验证码图片进行降噪处理
Dec 18 Python
Python3标准库之functools管理函数的工具详解
Feb 27 Python
django使用JWT保存用户登录信息
Apr 22 Python
使用Keras画神经网络准确性图教程
Jun 15 Python
django创建css文件夹的具体方法
Jul 31 Python
Python 必须了解的5种高级特征
Sep 10 Python
python爬虫中PhantomJS加载页面的实例方法
Nov 12 Python
Python开发之快速搭建自动回复微信公众号功能
Apr 22 #Python
Django小白教程之Django用户注册与登录
Apr 22 #Python
python中PIL安装简单教程
Apr 21 #Python
Python for Informatics 第11章之正则表达式(四)
Apr 21 #Python
Python for Informatics 第11章之正则表达式(二)
Apr 21 #Python
Python for Informatics 第11章 正则表达式(一)
Apr 21 #Python
编写Python爬虫抓取暴走漫画上gif图片的实例分享
Apr 20 #Python
You might like
浅谈php优化需要注意的地方
2014/11/27 PHP
百度工程师讲PHP函数的实现原理及性能分析(一)
2015/05/13 PHP
php简单判断文本编码的方法
2015/07/30 PHP
PHP实现链式操作的三种方法详解
2017/11/16 PHP
javascript replace方法与正则表达式
2008/02/19 Javascript
jQuery切换网页皮肤并保存到Cookie示例代码
2014/06/16 Javascript
JavaScript中对象property的读取和写入方法介绍
2014/12/30 Javascript
nodeJS代码实现计算交社保是否合适
2015/03/09 NodeJs
jquery实现的简单二级菜单效果代码
2015/09/22 Javascript
纯JavaScript代码实现文本比较工具
2016/02/17 Javascript
jQuery插件pagination实现无刷新分页
2016/05/21 Javascript
关于vuex的学习实践笔记
2017/04/05 Javascript
Angular+Node生成随机数的方法
2017/06/16 Javascript
Node.js利用js-xlsx处理Excel文件的方法详解
2017/07/05 Javascript
JavaScript实现QQ列表展开收缩扩展功能
2017/10/30 Javascript
基于nodejs res.end和res.send的区别
2018/05/14 NodeJs
Kettle中使用JavaScrip调用jar包对文件内容进行MD5加密的操作方法
2020/09/04 Javascript
JavaScript实现单点登录的示例
2020/09/23 Javascript
[09:34]2018DOTA2国际邀请赛寻真——永不放弃的iG
2018/08/14 DOTA
Python3使用requests包抓取并保存网页源码的方法
2016/03/15 Python
python常见排序算法基础教程
2017/04/13 Python
python-opencv在有噪音的情况下提取图像的轮廓实例
2017/08/30 Python
用Python实现筛选文件脚本的方法
2018/10/27 Python
DRF跨域后端解决之django-cors-headers的使用
2019/01/27 Python
Python列表list常用内建函数实例小结
2019/10/22 Python
安装并免费使用Pycharm专业版(学生/教师)
2020/09/24 Python
英国电器零售商:PRC Direct
2018/06/21 全球购物
优秀毕业生自我鉴定
2014/01/19 职场文书
网络技术专业求职信
2014/05/02 职场文书
2014年大学班长工作总结
2014/11/14 职场文书
大班下学期幼儿评语
2014/12/30 职场文书
颐和园英文导游词
2015/01/30 职场文书
教师节联欢会主持词
2015/07/04 职场文书
2016年中秋节寄语大全
2015/12/07 职场文书
《西游记》读后感(3篇)
2019/09/20 职场文书
Golang MatrixOne使用介绍和汇编语法
2022/04/19 Golang