Python爬虫实现抓取腾讯视频所有电影【实战必学】
前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
作者: Python新手学习之家
用python实现的抓取腾讯视频所有电影的爬虫

- # -*- coding: utf-8 -*-
- 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
- '''
- 在学习过程中有什么不懂得可以加我的python学习交流扣扣qun,934109170,群里有不错的学习教程与开发工具。
- '''
- #从电影分类列表页面获取电影分类
- 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
- 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爬虫实现抓取腾讯视频所有电影【实战必学】的更多相关文章
- 用python实现的抓取腾讯视频所有电影的爬虫
1. [代码]用python实现的抓取腾讯视频所有电影的爬虫 # -*- coding: utf-8 -*-# by awakenjoys. my site: www.dianying.atim ...
- 【Python3 爬虫】16_抓取腾讯视频评论内容
上一节我们已经知道如何使用Fiddler进行抓包分析,那么接下来我们开始完成一个简单的小例子 抓取腾讯视频的评论内容 首先我们打开腾讯视频的官网https://v.qq.com/ 我们打开[电视剧]这 ...
- 【转】Python爬虫:抓取新浪新闻数据
案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...
- Python爬虫:抓取新浪新闻数据
案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...
- Python爬虫,抓取淘宝商品评论内容!
作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...
- python爬虫:抓取下载电影文件,合并ts文件为完整视频
目标网站:https://www.88ys.cc/vod-play-id-58547-src-1-num-1.html 反贪风暴4 对电影进行分析 我们发现,电影是按片段一点点加载出来的,我们分别抓取 ...
- python爬虫数据抓取方法汇总
概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...
- python爬虫批量抓取ip代理
使用爬虫抓取数据时,经常要用到多个ip代理,防止单个ip访问太过频繁被封禁.ip代理可以从这个网站获取:http://www.xicidaili.com/nn/.因此写一个python程序来获取ip代 ...
- Python爬虫:抓取手机APP的数据
摘要 大多数APP里面返回的是json格式数据,或者一堆加密过的数据 .这里以超级课程表APP为例,抓取超级课程表里用户发的话题. 1.抓取APP数据包 表单: 表单中包括了用户名和密码,当然都是加密 ...
随机推荐
- C# 操作本地用户和组(基本全功能)
今天学习了下怎么用.Net操作本地用户和组,因为目前网上还没看到一篇比较完整的文章,所以整理了下也分享出来,最后附带参考文档,方便深究的童鞋继续学习.========== 原创作品 作者:Yo ...
- 如何查看当前linux服务器是否支持虚拟化
[root@localhost ~]# grep -E '(svm|vmx)' /proc/cpuinfo 或者: [root@localhost ~]# cat /proc/cpuinfo 找到fl ...
- 怎样在PaaS平台上搭建一个会自动关闭的会议室
首相得解释一下,什么叫做会自动关闭的会议室.我们的会议室是存在一个会议预定系统的,一般情况下,我们需要开会的时候,需要先抢占会议室.等待要开会的时候,去会议室里边开会,如果里边有别人,我们可以告诉他们 ...
- secureCRT连接虚拟机
1.secureCRT英文版下载 链接:https://pan.baidu.com/s/1LFWD-k2r4ZB7DHQA66QogQ 密码:khmo 破解方式参考 2.虚拟机静态IP设置 参考 3. ...
- dubbo分布式Service不可以创建Error creating bean with name 'XXXXXX'
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoService' ...
- Jquery才可以使用 this 指定当前DOM
Jquery才可以使用 this 指定当前DOM jquery获取并设置它的元素 <div class="shop-item" style="line-height ...
- ESP8266 智能配网 断电重连
ESP8266 智能配网 断电重连 #include <ESP8266WiFi.h> bool autoConfig() { WiFi.begin(); for (int i = 0; i ...
- 从0开始学前端(笔记备份)----HTML部分 Day1 HTML标签
- Netty创建服务器与客户端
Netty 创建Server服务端 Netty创建全部都是实现自AbstractBootstrap.客户端的是Bootstrap,服务端的则是ServerBootstrap. 创建一个 HelloSe ...
- ubuntu server 1604 关机和重启
命令有很多,记住以下两三个就够了 重启: sudo reboot (这个短,易记) sudo shutdown -r now 统一的shutdown形式 关机:sudo shutdown -P no ...