|
本帖最后由 xiaoxue 于 2021-3-10 09:21 编辑
1 m7 ?: l! z$ S
% M" o Z6 U6 J q, u5 d: \6 {7 \$ {9 b8 a' P3 A+ f3 x
- 3 r& Q" W8 Z. j2 Q/ ^! H: m
- # -*- coding: utf-8 -*-
\6 [( T' [1 w4 Y- d* a* Y - 0 b' q0 J/ M; J+ f8 k
- from aip import AipOcr
2 I8 e! n/ Z1 }; X1 d G! P! V - : Y% {! v% |; @1 T" }) z4 s
- from selenium import webdriver/ N: R4 }; X w- L! t* Q+ ]& [
- . f) d2 K6 u \/ x. P
- import time! |" b6 P+ m. g( @% y- f- T
- & v7 D {" Z5 S8 N/ j0 q, W
- import random/ a+ C8 i) ^% [' H2 k. h/ ?+ t
# S$ x! j; X/ D# p' t$ `7 _# X- import sys,re* \$ j, N: w; \ ^9 O
: o) G7 c$ v* O/ W5 H7 c5 a- from PIL import Image, ImageDraw,ImageFont
7 f5 \7 Y* p9 E/ N: k7 ~, E - 7 g$ n% Z: ~3 Z: k; c8 @. k
- """ 你的 APPID AK SK """! y& P$ d2 O& {1 h5 r8 T3 B' u
- 6 A: R( M3 _9 k; E& ~+ |) w9 s# r
- APP_ID = 'xxx'2 {, l2 `6 p- J2 |7 ^! V! d! G1 M
- 2 z$ y; h0 e! E+ o- E9 J4 W/ y
- API_KEY = 'xxx': J, \+ l+ K5 d: {- C: y; i% N
7 ~, Y2 L: S1 D; _! j) J5 ^! A- SECRET_KEY = xxx'
( ^% K4 I' S% z& ^ s$ d: a - * m! M+ ?4 m- \; N
- client = AipOcr(APP_ID, API_KEY, SECRET_KEY)2 x; |" T" K& j) }
! G6 V- f- Q a. @( v A; U0 @- #PROXY = "127.0.0.1:8118"
, `3 p, \3 g/ ?' j - ' K+ h- a- Q* C5 P
- chrome_options = webdriver.ChromeOptions()3 O( `" y( E9 x" q4 O, I/ I! ]
- 8 y [4 }+ j' @7 T
- chrome_options.add_argument('--headless')
, c/ E( T- D9 R. V" D - # w! V" ]: m4 _% Z! K- F( K+ G- n
- chrome_options.add_argument('--disable-gpu')+ t9 \1 h( R6 B2 ~
- : N' \' E9 \* q+ f0 }
- #chrome_options.add_argument('--proxy-server=%s' % PROXY)
0 z2 b$ S! A/ b! H+ F; X" I* G2 x - - H5 J/ z x& `6 G7 ?. D& N
- chrome_options.add_argument("--incognito")0 k- O1 d/ V. Y0 x
" U& R8 d8 c( H7 W+ e; w- chrome_options.add_argument('--ignore-certificate-errors')
; C0 R4 [0 L( V. z
: m$ }( {& J8 ~3 d3 c$ G* R8 Y- # Win" y! w" l( U6 I. o7 H; N8 I
- ]1 w5 c7 h# \; z: d9 ^. b
- # chrome_options.add_argument("--log-level=3")
2 r; ^# K8 G% Q
( C9 ]; J2 t& f- # chrome_options.add_argument("--disable-logging")
. _3 }% Z- Q' B+ b! Y6 J) k0 T - 7 i q9 o8 {# V" a7 G( I
- # chrome_options.add_argument("--disable-logging")
! i# T1 n. |" O. \5 z1 L. B4 A - 5 v- p( }3 P5 _* O
- #chrome_options.add_argument('--no-sandbox')
; g5 X; z5 j* A4 \- q' I) a2 F0 e$ ]0 C2 r( i - 0 Q8 O- m- L. @# T) A- v
- """ 读取图片 """
* Q0 f4 }$ u. {( X0 t7 d
& K" L7 T2 |* G8 C* w- def get_file_content(filePath):1 B3 B! h) N2 R0 Q2 n
- . O- E& A. I. d( N! k
- with open(filePath, 'rb') as fp:* \0 h/ ^8 ?3 b* c. \: Y3 M
- : M, q( A3 l+ V B) B
- return fp.read()& t7 ]5 P! T s5 E: g% c: g3 H% {
- 2 x7 }- s8 b4 F* U
- driver = webdriver.Chrome('~/chromedriver',chrome_options=chrome_options) d; J2 V; @2 k5 w
- , V2 n/ W* @( s. u3 x6 ?5 i6 C) j
- driver.set_window_size(1280, 727)
2 F* W9 l) R3 R) _0 q. Q5 D1 w - 4 x/ }9 |: B; P& m8 v# C4 m
- driver.get("https://youlikehits.com/")
% `7 `! z) b& [. `' m% Y) u% h
' N% }6 J8 m7 y4 W0 ]5 V" N- time.sleep(5)5 F& y8 Y4 W' V; D* L
- ; z, x3 k5 u1 ], s& w- x
- driver.find_element_by_id("username").send_keys('11111') #user0 s. ^& Y" u% u$ h* A* ] R; P
- 9 r: ^8 S' ^! ~3 J$ k8 Z
- driver.find_element_by_id("password").send_keys('111111') #password
# o$ X0 m4 H$ i9 A
3 `! w1 z6 f; j0 t- driver.find_element_by_xpath("//input[@value='Login']").click()
$ B1 l# K, m& d" i6 E& S1 J
, G C! N- _" `) H3 o |, h- driver.get("https://youlikehits.com/youtubenew2.php")
) c4 ^/ j: B. M: \2 F K1 U - 8 ], t1 h% l. W! b
- time.sleep(1)% ~/ Y% z# T3 F' C# O8 W8 e
- $ p% h- \2 y9 x/ J9 i3 y) n
- #Try Again
, \7 K* B5 L8 {% S- S. J - * c- y5 D& s6 x% H
- def checkRefresh(driver):: V6 O X' z3 ^$ f" D- C
- 5 p5 g K; ]9 W6 C5 K6 P+ k* D; y
- try:! G! B, D- x$ z0 Z0 e3 h
+ F) G, ^* k3 f- S# T- Refresh = driver.find_element_by_id('loadmore'): d# E# R! p0 ? B
8 T; n; m/ A& u3 |: b2 ^- Refresh.click()( A- M. X, i) x8 W; V d3 l# N
: p* j, s) _1 t- driver.set_window_size(1280, 727)
6 `; \9 r/ r+ p) U
6 m* L! v; T; q( O9 l* K, g- except Exception as e:
9 w* W+ g% e$ P$ z! ^
- E0 P* ], y3 m8 _- pass
) P) {# q; L/ X2 a3 [+ R3 ? - # ]8 U1 K# t4 i+ o
- def checkcaptcha(driver):9 Z) o' E N z- |
- , L3 u+ s7 ]8 d$ x0 W% ^5 [+ T/ {
- try:2 D/ `& R9 M8 J5 O+ ]9 x8 C
0 J) a+ S7 p4 B3 ?- captcha = driver.find_element_by_id('captcha')9 [% |% N. W+ H8 h( X
3 K, B! l# b9 z9 e3 \' s5 C9 H' P- print driver.get_window_size()- h7 K0 _: ^2 v! ?
- , Q" y) d. d. _/ m
- time.sleep(2)
9 Z+ y% p" N# A$ m8 e" u3 d - ' t S: e) [6 N9 j+ L
- driver.save_screenshot('/tmp/screenshot.png')2 D/ g/ V# W9 j' `
: q' M' B! W% q) V7 j6 z# ]: d1 j6 O- im = Image.open('/tmp/screenshot.png')
/ Q- ?( n( S, t0 T2 X: @ - & T1 z2 w. W8 P3 \# i# a
- #取消headless模式
b. n' a. o3 m5 [9 r
8 l7 R: [" r' M- #a = im.resize((1269, 610),Image.ANTIALIAS)2 b7 Z' X1 O. X& F5 z4 k
. `3 \/ P% b2 t0 F* d, w) k-
) O1 c$ g$ R6 _, l1 K# q
& u4 f+ x' w3 P3 M k9 Z7 T- #开启headless模式
1 X7 O$ L" L, I, r& Q+ u0 ]: m$ Q
& o# y4 C8 u' l2 \+ H8 N- a = im.resize((1269, 727),Image.ANTIALIAS). F, F; O" C" s+ c
- # L( ?$ W2 U0 n6 S; ?+ \: A$ U
- element = driver.find_element_by_xpath("//div['#captcha']//img[@src='captchayt.php']")
6 p) V% o2 o. N3 i( J - 9 b4 b, Q; b, Y/ S
- left = element.location['x']2 Q: n* u, n- w
; }8 `: z, Y+ e! h" q1 z8 @( q5 I- top = element.location['y']! K+ Z# `/ u, @" p7 U2 S9 t+ ?
- + i7 p" e! n( D; I( n; ~, w
- right = element.location['x'] + element.size['width']
' Q3 j1 I, e) J# i K2 q - 0 I' d o8 S" x. `
- bottom = element.location['y'] + element.size['height']0 k0 P& ?1 J, _7 c( X5 o3 [
- 5 ?% A- s+ `- k8 _, z
-
0 X' H' q, b! u! h3 n& T - " {% e+ o( c9 o1 `1 s5 U
- #element = driver.find_element_by_xpath("//div['#captcha']//img")
. B g/ e- S4 l0 T - 8 }5 z: D+ S/ O" W# j1 C+ j
- #driver.find_element_by_xpath("//div['#captcha']//img[@src='captchayt.php']").get_attribute("src")$ P3 w7 r. C& v O" W- j' B
7 i1 o. A! S# V* F2 I e- element = driver.find_element_by_xpath("//div['#captcha']//img[@src='captchayt.php']")
; \4 T1 n3 V8 L - , n# |4 V5 y1 @& H
- a.crop((left, top, right, bottom)).save('/tmp/screenshot1.png')
- b/ m; D: R* z$ _9 [9 s
! e2 t! c8 n M" Y$ ?- image = get_file_content('/tmp/screenshot1.png')
8 m0 d' \( D4 x7 H% m
" t9 a# S3 W: D; ]7 Y- a = client.basicGeneral(image)* a9 ]% L3 L( y- ?* n# i) Z; q
- * {1 E' I2 |3 L; a! W( d0 @6 o
- print a
y& s3 u! E% m0 W$ F9 \+ T - ! _' j' r! Z) h; E" C
- yzm = re.sub(r'\xd7',r'*',a['words_result'][0]['words']); G* J+ G- \& }" p! L; L0 C. Z
" q# s+ D0 _4 W8 [3 Y) t- yzm = re.sub(r'\xf7',r'/',yzm)( I3 D) D! C' s
- " E2 H: P: t" Q# U
- yzm = re.sub(r'x',r'*',yzm)
% r3 E4 [; X% [4 `! G - ) Y* ^. J2 C. K1 I& [1 _
- yzm = re.sub(r'X',r'*',yzm)
* R' @+ U0 m: z' Q- t# l
( g( J" p% R$ V6 N' G6 F8 Y3 A- yzm = eval(yzm)
3 a$ J: w5 c; r# A+ `) z
x/ Z* {" z7 N; |- #yzm = eval(yzm.replace('x','*').replace('÷','/')), Y$ K) ]+ C" a3 u' c
+ A9 G3 j. x0 ]0 w, |7 H- print '验证码: ' + str(yzm)
2 H$ [8 W) t4 `8 Z
, d" ]. k& e n9 \" U i- driver.find_element_by_name('answer').send_keys(yzm)2 o" [! u" T5 |+ W! I/ n3 O9 G' R% ^
6 E% r0 d$ B- \- |6 w# g- driver.find_element_by_name('submit').click()+ L; @. @! Y5 ?# H2 z5 @
, N9 v+ _& \! a! C: H9 g' t- time.sleep(3)1 P' B/ l- l$ X3 Y3 j
& L; _/ Q) B1 n5 B0 d* v- return 17 T+ B9 @1 k$ s
- 3 i& ?" l6 N) U; R* D" r u+ K3 Q
- except Exception as e:
+ Y) [4 D' V, d1 V" L ]
) D" @3 @# a# }8 c; f- return 0
( s! h! c. m1 f0 j3 N; e9 ? - 6 _+ K I& B3 c
- def followbutton(driver):# S7 e/ F5 H( s& b7 z
6 ?, \8 I3 I( M( z- try:% x9 w( l+ p( ]. g3 U2 ?
8 j+ F( q5 E" t% A& x- driver.switch_to_window(driver.window_handles[0])% J! e H+ B* C0 ^- n' q2 s
7 d, }' W4 y% @6 b- d- points = driver.find_element_by_id("currentpoints").text
% T: M1 V0 F7 O' e* B - 0 b% G7 I! x# v' x* F' ^4 Q
- driver.execute_script('return document.querySelector("a[class=followbutton]")').click()
' p( E" M% p/ m4 H% I
) x; E, _- J8 h2 c/ o- driver.switch_to_window(driver.window_handles[1])$ i$ u9 h# U( T; U2 K
. R( @; s" O* f' o& B0 f- VideoSource = ''.join(driver.page_source.split())4 V- K% B0 P$ G6 a; x5 h9 F
+ M3 m- A) l* r! x1 k5 \- if VideoSource.find("This") < 100 and VideoSource.find("This") >= 0:
- D+ u' ^# r$ } f$ r {, \# r - ( n; m; J l8 n* Y
- print VideoSource
& K- ]( j1 I+ B9 x( f6 h - % E- I T4 |% W% j7 C' A- q1 E, c
- driver.switch_to_window(driver.window_handles[0])
" a0 y, Y% N- ~0 r, j6 i
; E9 f3 c8 U5 q! ^; |: D) R; A- print "Refresh..."
, [6 T) i+ k( H; n6 l5 f1 h c
1 @" X+ S9 w: c, \3 e- driver.get("https://youlikehits.com/youtubenew2.php")
* I4 h9 s; z: S7 @$ d
2 X6 l+ h+ @5 x5 L/ z- time.sleep(1). n' C. `3 G, B6 W$ n1 s+ t
- & U2 G9 S' U7 t9 J' `& Q! L$ G0 Z3 r
- return points/ `* ?0 h$ s$ |- _$ j
, u! Q0 ~) D7 n1 u2 T7 `- except Exception as e:( q) n5 H4 c8 c9 J4 H M' ~
- ! W! I, a6 K3 @& s9 q
- return 09 A4 X6 |. d& t4 A- I n6 I
- / R# @+ Z1 r# f6 A+ X8 H
- for i in range(0,5000):& }6 G) c$ k0 \7 K* k
/ D% V) m8 `, R6 Z- try:
! S: ~( d+ A7 [; R( c, S - - `, K4 t8 C8 y2 n
- captcha = checkcaptcha(driver)6 D3 z0 s% g" L; @
- - {( a3 A5 p& q4 z$ @4 k
- time.sleep(1)3 X5 {* r# y$ N0 {
- y; y( u: X% g( [5 A" H+ f2 D% l8 C- checkRefresh(driver)! b% T; {* ~. J
- . g6 O5 f: B8 A- e- b6 s5 X
- points = followbutton(driver)
3 |' Q0 b/ o/ @1 }5 D; [3 L
0 w* x# g# {! J) U2 G5 T- u& i0 z- time.sleep(65)
% P+ X& O( T7 i2 [' n
) _1 [% _. L+ b! A" Y2 k- driver.switch_to_window(driver.window_handles[0])
6 A! w6 h# m+ R' R8 p: p - + h: ~) \6 }5 l" ]* _! ^
- tmpp = driver.find_element_by_id("currentpoints").text
Y; f% u2 d, q7 |9 g2 G
- i4 E8 ~- G2 ^- print "points: " + str(tmpp)
X5 A* R, C' |$ Q% {8 t
* m0 I3 n* Z8 C: W' z- if points == tmpp:! T% V2 n, [5 |4 f
) z! x* |" B0 g( o- print "Refresh...". u& u$ w3 t; \7 Q* {
- ) l6 ?7 Z) b6 L$ ?4 q) ^
- driver.get("https://youlikehits.com/youtubenew2.php")
. Z' A( G* ]* \2 o - : {$ k8 e+ M% n' k5 M, U* W
- time.sleep(5)
! M5 V. j% T0 w7 p! ?
' S) y3 G9 `) S4 |) e2 }4 m- except Exception as e:' ^0 f1 m) [1 a
~5 p M7 r, X% c. ?- driver.get("https://youlikehits.com/youtubenew2.php")- x% z/ l: z; c0 r. U" a9 L# R
- 4 V I3 I+ ~1 Q
- print 'error: ' + str(e)
% I. g7 g7 _! ]8 f8 p+ c1 p - 0 m6 T* |; J: K
- driver.quit()
7 ] {; ]( a' w
复制代码
C9 I9 f0 L' Y) `. ~/ {3 B$ N; g9 N! i7 P; }
. P B: ^1 k! c, ?* K" ] 因为headless ,可以挂在vps上跑。。 百度识别验证码正确率还是满高的。 ) {9 F5 @5 k8 A$ Z; }* W
' Y/ b1 B2 e f: _ |
评分
-
查看全部评分
|