在python3下使用requests,xpath,urllib爬取不得姐网站相关视频爬虫源代码
#coding=utf-8 from lxml import etree
import requests
import urllib
import os # 获取url的html等内容
def getHtml(url):
try:
kv = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}
r = requests.get(url, headers=kv, timeout=30)
r.encoding = 'utf-8'
return r.text except requests.URLError as e:
print('get html urlerror:{}'.format(e))
return '' except requests.HTTPError as e:
# code / reason / headers 异常
print('get html httperror:{}'.format(e))
return '' # 获取视频当前页视频url列表
def getVideoList(html):
try:
data = etree.HTML(html)
video_urls = data.xpath('//div[@class="j-video-c"]/div[@data-mp4]')
# print(type(video_urls[0]))
# print(dir(video_urls[0]))
# <a href="2" class="pagenxt">下一页</a>
next_page = data.xpath('//a[@class="pagenxt"]')
if next_page:
next_page = next_page[0].get('href') # videos[0].get('data-mp4')
return video_urls, next_page
# t(video_urls[0].get('data-mp4'))
except Exception:
print('lxml parse failed')
return None, None # urlretrieve()的回调函数,显示当前的下载进度
# a为已经下载的数据块
# b为数据块大小
# c为远程文件的大小
global myper def jindu(a, b, c):
if not a:
print("连接打开")
if c < 0:
print("要下载的文件大小为0")
else:
global myper
per = 100 * a * b / c if per > 100:
per = 100
myper = per
print("\r当前下载进度为:" + '%.2f%%' % per, end='')
if per == 100:
return True if __name__ == '__main__': path = os.path.join(os.path.abspath(os.path.curdir), 'videos')
if not os.path.exists(path):
os.mkdir(path)
url = "http://www.budejie.com/video"
next_url = url
n = 0
while True:
html = getHtml(next_url)
# print(html) videos, nextpage = getVideoList(html)
print('\n下载第{}页视频数据:{}'.format(n + 1, next_url))
# print(videos[0].get('data-mp4'))
if not videos:
break
for v in videos:
# if v:
video_url = v.get('data-mp4')
print('下载:{}'.format(video_url))
p = os.path.join(path, v.get('data-mp4').split('/')[-1]) if not os.path.exists(p):
try: #
# 使用request.build_opener 添加head可解决用urllib提示403错误 #
# myheaders = [('User - Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebK# it/525.17'\
# '# (KHTML, like Gecko) Version/3.1 Safari/525.17'),]
# opener = urllib.request.build_opener# ()
# opener.addheaders = myheaders #
# urllib.request.install_opener(opener)
urllib.request.urlretrieve(video_url, p, jindu)
except Exception:
print("\n下载文件:{}失败".format(video_url)) # 检测是否有下一页
if nextpage:
if nextpage == '1':
break
next_url = url + '/' + nextpage
else:
break
n = n + 1 print('所有数据抓取完毕!')
参考资料
在python3下使用requests,xpath,urllib爬取不得姐网站相关视频爬虫源代码的更多相关文章
- requests+xpath+map爬取百度贴吧
# requests+xpath+map爬取百度贴吧 # 目标内容:跟帖用户名,跟帖内容,跟帖时间 # 分解: # requests获取网页 # xpath提取内容 # map实现多线程爬虫 impo ...
- python3通过Beautif和XPath分别爬取“小猪短租-北京”租房信息,并对比时间效率(附源代码)
爬虫思路分析: 1. 观察小猪短租(北京)的网页 首页:http://www.xiaozhu.com/?utm_source=baidu&utm_medium=cpc&utm_term ...
- python3下BeautifulSoup练习一(爬取小说)
上次写博客还是两个月以前的事,今天闲来无事,决定把以前刚接触python爬虫时的一个想法付诸行动:就是从网站上爬取小说,这样可以省下好多流量(^_^). 因为只是闲暇之余写的,还望各位看官海涵:不足之 ...
- python3.6 利用requests和正则表达式爬取猫眼电影TOP100
import requests from requests.exceptions import RequestException from multiprocessing import Pool im ...
- Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫
如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:P ...
- Python使用urllib,urllib3,requests库+beautifulsoup爬取网页
Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...
- 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)
urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...
- Scrapy中用xpath/css爬取豆瓣电影Top250:解决403HTTP status code is not handled or not allowed
好吧,我又开始折腾豆瓣电影top250了,只是想试试各种方法,看看哪一种的方法效率是最好的,一直进行到这一步才知道 scrapy的强大,尤其是和selector结合之后,速度飞起.... 下面我就采用 ...
- 整理requests和正则表达式爬取猫眼Top100中遇到的问题及解决方案
最近看崔庆才老师的爬虫课程,第一个实战课程是requests和正则表达式爬取猫眼电影Top100榜单.虽然理解崔老师每一步代码的实现过程,但自己敲代码的时候还是遇到了不少问题: 问题1:获取respo ...
随机推荐
- vue2.0环境安装
参考网站http://www.open-open.com/lib/view/open1476240930270.html (以上博客vue init webpack-simple 工程名字<工程 ...
- script标签中type为"text/x-template"或"text/html"
写过一点前端的都会碰到需要使用JS字符串拼接HTML元素然后append到页面DOM树上的情况,一般的写法都是使用+号以字符串的形式拼接,如果是短点的还好,如果很长很长的话就会拼接到令人崩溃了. 比如 ...
- retrofit 使用解析
retrofit 基础入门:https://www.jianshu.com/p/b64a2de066c3 retrofit 使用详解:https://blog.csdn.net/carson_ho/a ...
- Maven入门-运行struts项目进行测试(三)
maven运行struts项目进行测试: 在入门二中已经导入struts的jar包. 此时的pom.xml文件 <project xmlns="http://maven.apache. ...
- 【黑客免杀攻防】读书笔记7 - 软件逆向工程基础1(函数调用约定、Main函数查找)
0x1 准备工作 1.1.准备工具 IDA:交互式反汇编工具 OllyDbg:用户层调试工具 Visual Studio:微软开发工具 1.2.基础知识 C++开发 汇编语言 0x2 查找真正的mai ...
- ARMV8 datasheet学习笔记4:AArch64系统级体系结构之编程模型(1)-EL/ET/ST
1.前言 ARMV8系统级编程模型主要包括异常级别.运行状态.安全状态.同步异常.异步异常.DEBUG 本文主要对系统级编程模型做一个概要介绍 2. 异常级别 2.1 Exception level概 ...
- eclipse 反编译
Eclipse Class Decompiler安装此插件,可以编译源代码且调试
- (并发编程)全局解释器锁(GIL)-----有了GIL不用给线程加锁了?
一.全局解释器锁 (GIL)运行test.py的流程:a.将python解释器的代码从硬盘读入内存b.将test.py的代码从硬盘读入内存 (一个进程内装有两份代码---一份cpython解释器代码 ...
- Tpcc-MySQL对mysql数据库进行性能测试报告、分析及使用gnuplot生成图表展示
TPC-C是专门针对联机交易处理系统(OLTP系统)的规范,一般情况下我们也把这类系统称为业务处理系统. tpcc-mysql是percona基于TPC-C(下面简写成TPCC)衍生出来的产品,专用于 ...
- 批量监测dns是否可用脚本,不可用时并切换
#!/usr/bin/env python # coding=utf-8 # hexm@2016-02-14 import time import requests import paramiko i ...