#!/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. python基础训练营03——字典、集合、判断、循环

    一.字典dict: 相比列表list而言,列表list像一本书,如果要查书中的某一个内容,需要把书从前往后翻一遍,直到找到想要获取的东西:而字典dict,就像现实中的字典一样,通过查找特定的字或者词( ...

  2. 学习人工智能的第六个月[深度学习[Deep Learning,DL]]

    这个月阅读了论文[Partial Adversarial Domain Adaptation-eccv18],文章着眼于源域标签空间包含目标域标签空间的场景,在域对抗神经网络的基础上提出了部分对抗域适 ...

  3. 容器基础(十): 使用kubernetes部署应用

    概述 使用之前的脚本(env/server.py 得到 env/server:v0.1 镜像, env/worker.py 得到 env/worker:v0.1)得到的镜像,在部署好kubernete ...

  4. HTML5的 input:file上传类型控制(转载)

    http://www.haorooms.com/post/input_file_leixing HTML5的 input:file上传类型控制 2014年8月29日 66352次浏览 一.input: ...

  5. web四则运算

    目录 1.coding.net地址 2.PSP 3.Information Hiding, Interface Design, Loose Coupling 4.计算模块接口的设计与实现过程 5.计算 ...

  6. DDD-领域驱动设计

    识别领域事件 DDD战术篇:领域模型的应用 DDD战略篇:架构设计的响应力 DDD实战篇:分层架构的代码结构

  7. 【Python】Python中子类怎样调用父类方法

    python中类的初始化方法是__init__(),因此父类子类的初始化方法都是这个,如果子类不实现这个函数,初始化时调用父类的初始化函数,如果子类实现这个函数,就覆盖了父类的这个函数,既然继承父类, ...

  8. 【bzoj3626】[LNOI2014]LCA 树链剖分+线段树

    题目描述 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q次询问,每次询 ...

  9. 【BZOJ 3643】Phi的反函数 数搜索

    这道题是典型的数搜索,讲究把数一层一层化小,而且还有最重要的大质数剪枝. #include <cstdio> #include <cmath> typedef long lon ...

  10. WordPress后台edit-tags.php里无限栏目分类实现

    在 WordPress 里 http://localhost/wordpress3.6.1/wp-admin/edit-tags.php?taxonomy=category 这个链接可以显示 WP 里 ...