#!/usr/bin/env python
# encoding: utf-8
import requests
from random import choice
from lxml import html
from urllib.parse import urljoin,quote
import os
import time
NAMEURLDIC={}
NAMEURLDIC_L2={}
ualist=["Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
"Mozilla/5.0 (compatible; Yahoo! Slurp/3.0; http://help.yahoo.com/help/us/ysearch/slurp)"]
ua=choice(ualist)
header={"User_Agent":ua}
mailurl="https://press.mu"
url="https://press.mu/tag"
searc_url="https://press.mu/search/{}?p={}"
def getpage(url):
req=None
try:
req=requests.get(url=url,headers=header,stream=True)
req.encoding=req.apparent_encoding
except:
pass
return req
def parse(url):
source=getpage(url).text
if len(source):
root=html.fromstring(source)
return root
def buff(url):
buff = None
req=getpage(url)
return req
def save_file(title,url,type="m3u8"): if os.path.exists("pressimg"):
pass
else:
os.mkdir("pressimg")
with open(f'./pressimg/{title}.{type}',"wb") as fs:
fs.write(buff(url).content) root=parse(url)
taglist=root.xpath("//section[@id='tag']/ul/li/a")
for tag in taglist:
title=tag.xpath("./text()")[0]
href=urljoin(mailurl,tag.xpath("./@href")[0])
NAMEURLDIC.setdefault(title,href)
for k,v in NAMEURLDIC.items():
#第一页
root=parse(v)
#视频件数:
v_count=root.xpath("//p[@id='hit']/strong/text()")[0]
v_max_page_num=root.xpath("//nav[@id='pager']/ul/li[last()-1]/a/text()")[0]
print(f'当前分类为{k}:,视频件数为:{v_count}')
for item in range(1,int(v_max_page_num)+1):
print(f"获取第{item}页")
if item==1:
pass
else:
root = parse(searc_url.format(quote(title.strip()),item))
level2list=root.xpath("//section[@class='items']//h2/a")
for level2 in level2list:
title_level2 = level2.xpath("./text()")[0]
href_level2 = urljoin(mailurl, level2.xpath("./@href")[0])
NAMEURLDIC_L2.setdefault(title_level2, href_level2)
print(title_level2,href_level2)
root2 = parse(href_level2)
videourl=root2.xpath("//div[@id='player']//video/source/@src")[0]
imgurl="https:"+root2.xpath("//div[@id='player']//video/@poster")[0]
print("videourl",videourl)
print("imgurl",imgurl)
save_file(title_level2,videourl)
save_file(title_level2,imgurl,"jpg")
print("开始下载",f"{title_level2}.jpg")

  

pressmuSpiderr的更多相关文章

  1. Thymeleaf3.0内容

    Thymeleaf简介 什么是Thymeleaf Thymeleaf是网站或者独立应用程序的新式的服务端java模板引擎,可以执行HTML,XML,JavaScript,CSS甚至纯文本模板. Thy ...

随机推荐

  1. flask - 1

    from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Worl ...

  2. intelliJ idea 使用技巧&方法

    导入的项目查看svn地址:在项目上右键--subversion--relocate可以看到以前对应的svn地址. 重要的几个快捷键的使用方式: (1)       alt+insert 成员变量封装 ...

  3. red入门学习笔记

    删除以name开头的所有键值. 查找开头和结尾相同,中间字符不同

  4. [剑指Offer] 10.矩形覆盖

    题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? [思路]可归纳得出结论: f(n) = f(n-1) + f ...

  5. JAVA高并发处理------多线程

    线程安全概念:当多个线程访问某一个类(对象或方法)时,这个对象始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的. 分析:当多个线程访问myThread的run方法时,以排队的方式进行处 ...

  6. flask-sqlalchemy 用法总结

    Flask-SQLAlchemy是一个Flask扩展,能够支持多种数据库后台,我们可以不需要关心SQL的处理细节,操作数据库,一个基本关系对应一个类,而一个实体对应类的实例对象.Flask是一个轻量级 ...

  7. 【题解】CQOI2012交换棋子

    感受到网络流的强大了……这道题目的关键在于: 前后颜色不变的,流入流出的次数相等:原本是黑色的最后变成了白色,流出比流入次数多1:原本是白色最后变成黑色,流入比流出次数多一.所以我们将每一点拆成3个点 ...

  8. [IOI2007 D1T1]Miners 矿工配餐

    题目大意:有$2$个煤矿,$n$天.每天给一个煤矿送餐(共有有$3$种餐),价值为它与前面两次送餐(如果有的话)不同的种类数.最大化价值. 题解:看到只有三种餐,考虑状压$DP$.$f_{i,j,k, ...

  9. 12.25模拟赛T2

    https://www.luogu.org/blog/a23333/post-xing-xuan-mu-ni-sai-path-ji-wang-zui-duan-lu 如果设f[i]表示从i到n的期望 ...

  10. 如何使用Photoshop制作真实的尺子

    前言: 日常生活中经常性的偶尔需要测量一些东西的尺寸,但刚好手头上缺乏尺子等必要的测量工具,这时候其实我们可以利用Photoshop,临时制作一把基于现实物理单位(如:厘米)的虚拟尺子. 难点: 像素 ...