数学建模社区-数学中国

标题: Python-追剧下载视频 [打印本页]

作者: 檀俾九    时间: 2021-1-19 10:25
标题: Python-追剧下载视频
[code=python]#!/usr/bin/python
; k) X! w- [. p1 u' W# -*- coding: utf-8 -*-
4 G9 y7 ?3 F1 f! h1 ]; A# T5 r1 ~  {: P/ d& T
import threading
- U+ P( H9 N% U+ S  Ffrom lxml import etree$ ^" n& V" P9 x6 {
import requests( |+ a/ p# {& G1 V  U
from ctypes import windll; X4 f; m+ L1 Y* D% m, |  V4 d+ }
- I9 k8 L- ^1 ]( @4 E5 d
* t- B. D2 ^' \" h& K) ~- g
def getmidstring(html, start_str, end):
& O( N4 I  G! y9 L# z    start = html.find(start_str)
7 P) G" a, a8 C4 y    if start >= 0:& c; `% g% Q9 l# f: {. S
        start += len(start_str)
! u2 X# M" A# U, J/ j        end = html.find(end, start)
3 L6 k+ u* m. Y2 F1 n* f! K        if end >= 0:
5 k# _2 E2 z( X1 n/ R# Q  ~2 }1 n            return html[start:end].strip()
2 G* ~) l2 v* ^# Q, u8 }, H; O, e2 [$ S6 [; N

; X- I8 t& @. [8 k6 f) t9 pdef getleftstring(html, end):
6 c4 ]  d. B8 D' q( J0 c- o    end = html.find(end)
: a6 H8 X9 y( Q) H    if end >= 0:5 V, v; m' g0 D
        return html[0:end].strip()
+ X- q) \' s9 j
2 c% p/ _& H4 A& N& X( B7 ~. v3 i! C, {- v" u  Y( P
def updata(i):
; j$ D( ?8 Z3 x+ u' A0 p2 P! R/ a( G; u    req = requests.get("https://www.xxx.com/play/xxx-2-%s.html" % i)/ o6 o1 H( w1 h: [
    req.encoding = "utf-8"# I+ G8 P9 X, d  r# d# A
    m3u8 = getmidstring(req.text, "var now=\"", "\"")
' _3 _! j1 L# n, b) N  [
2 @6 A. R1 G& ~    html = etree.HTML(req.text)' F; @$ k5 m# C
    title = html.xpath("//meta[@name=\"description\"]/@content")[0]
: P' ]# F3 m3 e/ j7 G$ E    title = getleftstring(title, ",")- K& D) e, N8 c, }6 L/ l: P
    print(title)2 g8 V0 R% u/ k2 b  `# s) R

- z  k; q  G- d( ]( ^) ^    cmd = "\"%s\" --workDir \"%s\" --saveName \"%s\" --enableDelAfterDone" % (
3 `" Q3 `0 L' Z0 `# x        m3u8,+ f0 N( [4 G6 X% T: Z
        r"F:\xxx",
* }8 K! ~- A* e' W/ L; a        title)
# Y( u5 @+ n' x    print(cmd)
6 u# W: H& J2 r; z1 V" o
( a# [: k. b# |6 k# r2 J# w    dll = windll.LoadLibrary("Shell32.dll")# {( _+ W& H, @. Z: U1 X. j, e. L
    print(dll.ShellExecuteW(None,' u/ h) w7 i. X1 K% n- g! O
                            "open",
2 @- Y5 l3 _. M2 g                            r"D:\xxx.exe",$ L* B! O  n2 p# H: J
                            cmd, None, 1))6 a" _! p/ y& `3 {+ H

  O/ F- f" s5 y  X- a: I
% q/ w# C  r1 W2 n+ zdef cycle():
& y0 c* G$ R. t, e# N; t) C4 N/ k5 w    for i in range(40):
! x2 Y; {& ]. d/ m- N        updata(i)
; d( T9 G; |  C* }- O9 D
# N9 \# C; ^- \0 g- ?; |8 d; T$ D, m
: A# z1 Z( v9 e' L( {" qif __name__ == '__main__':. B+ ^* j$ W* ~/ T# ^9 b1 X
    hThread = threading.Thread(target=cycle)
" W, ~, e; g! Q( m    hThread.setDaemon(True)- M- r4 K. w' a$ _' w" F4 _
    hThread.start()$ C% C4 H" [0 q! v6 B6 h& r
    hThread.join()6 O5 P, v" F' K8 U8 u  G1 A
[/code]
- z2 q0 R4 \5 T, {  u# V# ^( \. \7 P+ S! `' b& y
在线追剧又卡又慢!还不方便,写个Python脚本自动帮我缓存下~+ H" f3 I: ?' N
( Z% q5 Q+ \; i: v) e

! g6 Z8 v/ `6 H2 f; k5 K  J: r
转发自派生社区
Python交流群:1047602540

+ r' H& `1 R- p1 I* v
' q+ ]4 u( U: {1 m, c) y4 l1 }, H1 D& P  H( B) h- X! P/ |0 H1 ]





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5