1. 定向爬取极客学院视频,原本只有年费VIP只能下载,经过分析,只要找个免费体验VIP即可爬取所有视频
  2. 涉及的基本技术:python xpath 正则 com+
  3. 通过python调用迅雷从组件,实现自动创建文件夹和自动添加批量下载任务,前提要成功安装迅雷和迅雷组件
  4. 思路:path路径爬取所有标签-》搜索页面所有该课程分类-》课程页面获取课程明细-》正则分析视频地址
  5. 极客学院的一直在改进,可能需要自己改进

import requests from lxml import etree import re import sys, os, glob,time import scrapy

reload(sys) sys.setdefaultencoding("utf-8")

#baesurl = "http://www.jikexueyuan.com/search/s/q_"
#base_path = "f:/jike/"

#heanders Cookie需要自己抓取,否则只能抓取到免费课程

headers = { "Host": "www.jikexueyuan.com", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8", "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3", "Accept-Encoding": "gzip, deflate", "Cookie": "ga=GA1.2.1700377703.1438173034; Hmlvtf3c68d41bda15331608595c98e9c3915=1438173034; MECHATLVTime=1438179151498; MECHATCKID=cookieVal=006600143817303272961295; statssid=1438985023415; statuuid=1438173038588973692017; connect.sid=s%3AWt8IWWxkVZ6zlhop7HpbG-vtXqtwIAs.QC1tYy4qV1bHOMDN0UTUfScLKFncl4NY5zAk1SS17Kw; QINGCLOUDELB=37e16e60f0cd051b754b0acf9bdfd4b5d562b81daa2a899c46d3a1e304c7eb2b|VbjfT|VbjfT; Hmlpvtf3c68d41bda15331608595c98e9c3915=1438179151; statisNew=0; statfromWebUrl=; gat=1; uname=jike76; uid=2992598; code=SMapFI; authcode=d572TzIvHFXNIVNXcNf4vI5lv1tQlyEknAG4m0mDQmvMRPa4VhDOtJXOSfO%2BeVFVPzra8M1sEkEzxqLX9qRgS6nWhd5VMobbDpeqvJ726i54TqMoDo81P4OlhQ", "Connection": "keep-alive" }

class jikeautodown: basepath = "" baseurl = "" coursetag = "" courseid = ""

def __init__(self, base_path, base_url):
if base_path and base_url:
self.base_path = base_path
self.base_url = base_url
self.get_tags()
else:
print("base_path and base_url is all must needed!")
return def run(self):
self.get_tags()

get_tags 获取所有便签

def get_tags(self):
url = "http://www.jikexueyuan.com/path/"
tag_html = requests.get(url).text.decode("utf-8").encode("GB18030")
tag_etree = etree.HTML(tag_html)
tag_lists = [str(tag).rstrip("/")[str(tag).rstrip("/").rindex("/") + 1:] for tag in
tag_etree.xpath('/html/body/div[1]/div[4]/div/div[3]/div/a/@href') if tag]
if tag_lists:
for tag in tag_lists:
print(tag)
self.course_tag = tag
self.get_total_page(tag)

get_tags 获取课程所有页面 课程分页是js生成不好直接抓取,所以就暴力了

def get_total_page(self, tag):
if tag:
for page in range(1, 50):
page_url = self.base_url + tag + "?pageNum=%d" % page
# print(page_url)
page_html = requests.get(page_url, headers=headers).text.decode("utf-8").encode("GB18030")
# print(page_html)
no_userMenu = re.search(r"userMenu", page_html, re.S)
if no_userMenu is None:
print("please check the cookies")
return
no_search = re.search(r"no-search", page_html, re.S)
if no_search:
print("the tag ;%s,%d is biggest page" % (tag, page - 1))
# return page_url_lists
break
else:
# page_url_lists.append(page_url)
self.get_course_pages(page_url)
# print(page_url)

getcoursepages 获取课程详细页面

def get_course_pages(self, tag_url):
if tag_url:
print("the tag_url:%s " % tag_url)
course_page_lists = self.get_xpath_lists(tag_url, headers,
'//*[@id="changeid"]/ul/li/div/div[2]/h5/a/@href')
if course_page_lists:
for course_page_url in course_page_lists:
self.get_down_urls(course_page_url)

getdownurls通过正则获取视频下载地址

def get_down_urls(self, course_page_url):
if course_page_url:
self.course_id = course_page_url[course_page_url.rindex("/") + 1:course_page_url.rindex(".")]
# print(course_page_url)
print(" course_id:%s %s" % (self.course_id, course_page_url))
course_down_lists = self.get_xpath_lists(course_page_url, headers,
'//*[@class="video-list"]/div[2]/ul/li/div/h2/a/@href')
if course_down_lists:
for course_down_url in course_down_lists:
course_down_html = requests.get(course_down_url, headers=headers).text.decode("utf-8").encode(
"GB18030")
course_down = re.findall(r'source src="(.*?)"', course_down_html, re.S)
if course_down:
print(" %s" % course_down[0])
if self.addTasktoXunlei(course_down[0]):
# print(" %s is add success!" % course_down[0])
print(" is add success!")
time.sleep(5)

getfilelists创建文件夹

def get_file_lists(self, course_tag, course_id):
course_path = ""
if self.base_path and os.path.exists(self.base_path) == False:
try:
os.mkdir(self.base_path)
except Exception:
print("error :%s" % Exception.message)
return
if course_tag and os.path.exists(self.base_path + course_tag) == False:
try:
os.mkdir(self.base_path + course_tag)
# print("%s dir is create success!" % (self.base_path + course_tag))
except Exception:
print("dir is create error,the error is %s" % Exception.message) tmp = self.base_path + course_tag + "\\" + str(course_id)
if course_id and os.path.exists(tmp) == False:
try:
os.mkdir(tmp)
course_path = tmp
# print("%s dir is create success!" % tmp)
except Exception:
print("dir is create error,the error is %s" % Exception.message)
return
else:
course_path = tmp
return course_path

getxpathlists 专门解析xpath,不用每次都写

def get_xpath_lists(self, url, headers, xpath):
try:
html = requests.get(url, headers=headers).text.decode("utf-8").encode("GB18030")
tree = etree.HTML(html)
lists = [str(plist) for plist in tree.xpath(xpath) if plist]
except Exception:
print("get xpath list is error is :%s" % Exception.message)
return
return lists

addTasktoXunlei 添加迅雷任,必须安装迅雷,还需要对迅雷设置默认不提醒,否则就需要手动点击确定了

def addTasktoXunlei(self, down_url):
flag = False
from win32com.client import Dispatch
o = Dispatch("ThunderAgent.Agent.1")
# http: // cv3.jikexueyuan.com / 201508011650 / a396d5f2b9a19e8438da3ea888e4cc73 / python / course_776 / 01 / video / c776b_01_h264_sd_960_540.mp4
if down_url:
course_infos = str(down_url).replace(" ", "").replace("http://", "").split("/")
course_path = self.get_file_lists(self.course_tag, self.course_id)
try:
o.AddTask(down_url, course_infos[len(course_infos)-1], course_path, "", "http://cv3.jikexueyuan.com", 1, 0, 5)
o.CommitTasks()
flag = True
except Exception:
print(Exception.message)
print(" AddTask is fail!")
return flag if __name__ == "__main__":
myjike = jike_auto_down("f:\\jike\\", "http://www.jikexueyuan.com/search/s/q_")
myjike.run()

python极客学院爬虫V1的更多相关文章

  1. python scrapy版 极客学院爬虫V2

    python scrapy版 极客学院爬虫V2 1 基本技术 使用scrapy 2 这个爬虫的难点是 Request中的headers和cookies 尝试过好多次才成功(模拟登录),否则只能抓免费课 ...

  2. 基于requests实现极客学院课程爬虫

    背景 本文主要是为了完成极客学院课程<Python 单线程爬虫>中讲师布置的实战作业. 开发环境 操作系统:windows 10 Python :Python 2.7 IDE:PyChar ...

  3. 【极客学院出品】Cocos2d-X系列课程之九-BOX2D物理引擎

    Cocos2d-x 是时下最热门的手游引擎,在国内和国外手机游戏开发使用的份额各自是70%和25%,在App Store的top10中,有7个是用它开发的. 本节课程为Cocos2d-x系列课程之九, ...

  4. scrapy爬取极客学院全部课程

    # -*- coding: utf-8 -*- # scrapy爬取极客学院全部课程 import scrapy from pyquery import PyQuery as pq from jike ...

  5. 国内第一本micropython的书出版《机器人Python极客编程入门与实战》

    第一本micropython的书<机器人Python极客编程入门与实战>. 购买地址:https://item.taobao.com/item.htm?spm=2013.1.w4018-1 ...

  6. 【极客学院-idea教程】

    极客学院idea教程: http://whudoc.qiniudn.com/2016/IntelliJ-IDEA-Tutorial/index.html

  7. maven介绍 极客学院

    来自极客学院 Apache Maven 是一套软件工程管理和整合工具.基于工程对象模型(POM)的概念,通过一个中央信息管理模块,Maven 能够管理项目的构建.报告和文档. Maven - 概述 M ...

  8. 极客学院年VIP卡原价260的F码,200出售

    F码是中国最大的IT职业在线教育平台——极客学院推出的VIP时间兑换码,凭此可在极客学院官网兑换年VIP,畅享平台上所有IT技术课程. 购买请点击 http://www.bejson.com/othe ...

  9. 极客学院免费VIP

    [手快福利]用我的链接注册极客学院,你我都能免费得30天VIP!6500+编程开发视频教程随便学,还能下载资料和源码 http://e.jikexueyuan.com/invite/index.htm ...

随机推荐

  1. 【Linux_Fedora_应用系列】_3_如何利用Smplayer播放WMV格式的文件

    在上一篇我们成功安装了视频播放器,并且成功安装里解码器[Linux_Fedora_应用系列]_2_如何安装视频播放器和视频文件解码 安装完的Smplayer的GUI的界面程序,可以播放FLV.AVI. ...

  2. InfluxDB学习系列教程,InfluxDB入门必备教程

    nfluxDB是一个当下比较流行的时序数据库,InfluxDB使用 Go 语言编写,无需外部依赖,安装配置非常方便,适合构建大型分布式系统的监控系统. 本文是一系列InfluxDB学习教程的目录,现主 ...

  3. Ionic 开发环境搭建

    android sdk环境搭建并非易事,本人经过无数失败,才使用以下方式成功 配置Ionic 开发环境 1.下载JDK并配置Java运行环境 http://www.oracle.com/technet ...

  4. $('div a') 与$('div>a'),.div+.div2与.div~.div2

    $('div a'):div标签下所有层次a元素的jquery对象 $('div>a'):div标签下子元素层次a元素的jquery对象 <body> <div class=' ...

  5. emberjs学习一(环境和第一个例子)

    code { margin: 0; padding: 0; white-space: pre; border: none; background: transparent; } code, pre t ...

  6. Mysql关键字 Mysql保留字列表 Mysql字段名

    Mysql保留字列表.吠品整理. 尝试使用一个识别符,例如使用嵌入式MySQL 数据类型或函数名作为表名或列名,例如TIMESTAMP 或GROUP,会造成一个常见问题.允许你这样操作( 例如,ABS ...

  7. mysql支持IOS的Emoji表情

    原因: UTF-8编码有可能是两个.三个.四个字节.Emoji表情是4个字节,而Mysql的utf8编码最多3个字节,所以数据插不进去. 解决办法: 将Mysql的编码从utf8转换成utf8mb4 ...

  8. 【问题】js 改变鼠标样式,chrome浏览器不能立即更新,暂没有解决办法

    元素的css,cursor可以改变鼠标样式.也就是鼠标放到元素上去时,改变为相应状态. 通过JS改变cursor时,我发现chrome浏览器不能立即更新,需要动一下鼠标才行,试了几个其它浏览器都是立即 ...

  9. .NET破解之图片下载器

    自去年五月加入吾爱后,学习了三个月,对逆向破解产生了深厚的兴趣,尤其是对.NET方面的分析:但由于这一年,项目比较忙,事情比较多,破解这方面又停滞了许久,不知道还要好久. 前些天,帮忙批量下载QQ相册 ...

  10. SharePoint 2013 VSS 编写器

    Windows Server 包含的 VSS 是提供内置卷影复制功能的基础结构.VSS 创建的卷影副本扩展了存储管理员的磁带备份存档解决方案,提供可轻松.有效创建和还原的高保真时间点副本,从而帮助简化 ...