用python实现的抓取腾讯视频所有电影的爬虫
1. [代码]用python实现的抓取腾讯视频所有电影的爬虫
# -*- 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>http://www.enterdesk.com/special/shouhui/
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爬虫实现抓取腾讯视频所有电影【实战必学】
2019-06-27 23:51:51 阅读数 407 收藏 更多 分类专栏: python爬虫 前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问 ...
- 【Python3 爬虫】16_抓取腾讯视频评论内容
上一节我们已经知道如何使用Fiddler进行抓包分析,那么接下来我们开始完成一个简单的小例子 抓取腾讯视频的评论内容 首先我们打开腾讯视频的官网https://v.qq.com/ 我们打开[电视剧]这 ...
- Hello Python!用 Python 写一个抓取 CSDN 博客文章的简单爬虫
网络上一提到 Python,总会有一些不知道是黑还是粉的人大喊着:Python 是世界上最好的语言.最近利用业余时间体验了下 Python 语言,并写了个爬虫爬取我 csdn 上关注的几个大神的博客, ...
- Python爬虫实战:爬取腾讯视频的评论
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 易某某 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- python 爬取腾讯视频的全部评论
一.网址分析 查阅了网上的大部分资料,大概都是通过抓包获取.但是抓包有点麻烦,尝试了F12,也可以获取到评论.以电视剧<在一起>为例子.评论最底端有个查看更多评论猜测过去应该是 Ajax ...
- 测试开发Python培训:抓取新浪微博抓取数据-技术篇
测试开发Python培训:抓取新浪微博抓取数据-技术篇 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的se ...
- 测试开发Python培训:抓取新浪微博评论提取目标数据-技术篇
测试开发Python培训:抓取新浪微博评论提取目标数据-技术篇 在前面我分享了几个新浪微博的自动化脚本的实现,下面我们继续实现新的需求,功能需求如下: 1,登陆微博 2,抓取评论页内容3,用正则表 ...
- Python 3.6 抓取微博m站数据
Python 3.6 抓取微博m站数据 2019.05.01 更新内容 containerid 可以通过 "107603" + user_id 组装得到,无需请求个人信息获取: 优 ...
- [python应用]python简单图片抓取
前言 emmmm python简单图片抓取 1 import requests 2 import threading 3 import queue 4 from subprocess import P ...
随机推荐
- jquery的push()
JavaScript push() 方法 JavaScript Array 对象 定义和用法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. 语法 arrayObject.pus ...
- Android环境变量的设置(详细图解版)
分类: Android初学学习笔记2011-07-10 09:47 99479人阅读 评论(0) 收藏 举报 androidtoolspathcmd 查阅了网上很多的资料但是对于环境变量设置介绍的不够 ...
- Quartz.Net - Lesson2: 任务和触发器
Lesson 2: 任务和触发器 本系列文章是官方3.x文档的翻译,原文地址:https://www.quartz-scheduler.net/documentation/quartz-3.x/tut ...
- android 常用方法总结
public class Toolkit { /** * * Role:Telecom service providers获取手机服务商信息 <BR> * * 需要加入权限<uses ...
- RecyclerView 踩坑
一.RecyclerView设置拖动后怎么监听拖动的开始和结束 ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Cal ...
- centos7 修改sudoers文件
使用root账户用 visudo 命令来修改. 转自: https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers ...
- 实战c++中的vector系列--creating vector of local structure、vector of structs initialization
之前一直没有使用过vector<struct>,如今就写一个简短的代码: #include <vector> #include <iostream> int mai ...
- Discrete Function(简单数学题)
Discrete Function There is a discrete function. It is specified for integer arguments from 1 to N (2 ...
- ajax (异步js+xml)
AJAX 基础 AJAX = 异步js+xml 通过与后台服务器进行少量数据交换,实现前台网页局部更新 XMLHttpRequest对象 是 AJAX 的基础 var xmlhttp; if (win ...
- cocos2dx使用cocostudio导出的ui
local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...