前言:
土福曾說,百度指數(shù)很難抓,在淘寶上面是20塊1個關(guān)鍵字:
哥那么叼的人怎么會被他嚇到,于是乎花了零零碎碎加起來大約2天半搞定,在此鄙視一下土福
安裝的庫很多:
1
2
3
4
5
6
7
8
9
10
11
|
谷歌圖像識別tesseract-ocr pip3 install pillow pip3 install pyocr selenium2.45 Chrome47.0.2526.106 m or Firebox32.0.1 chromedriver.exe |
圖像識別驗證碼請參考:http://www.jfrwli.cn/article/101416.html
selenium用法請參考:http://www.jfrwli.cn/article/103695.html
進(jìn)入百度指數(shù)需要登陸,登陸的賬號密碼寫在文本account里面:
萬能登陸代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# 打開瀏覽器 def openbrowser(): global browser # https://passport.baidu.com/v2/?login url = "https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" # 打開谷歌瀏覽器 # Firefox() # Chrome() browser = webdriver.Chrome() # 輸入網(wǎng)址 browser.get(url) # 打開瀏覽器時間 # print("等待10秒打開瀏覽器...") # time.sleep(10) # 找到id="TANGRAM__PSP_3__userName"的對話框 # 清空輸入框 browser.find_element_by_id( "TANGRAM__PSP_3__userName" ).clear() browser.find_element_by_id( "TANGRAM__PSP_3__password" ).clear() # 輸入賬號密碼 # 輸入賬號密碼 account = [] try : fileaccount = open ( "../baidu/account.txt" ) accounts = fileaccount.readlines() for acc in accounts: account.append(acc.strip()) fileaccount.close() except Exception as err: print (err) input ( "請正確在account.txt里面寫入賬號密碼" ) exit() browser.find_element_by_id( "TANGRAM__PSP_3__userName" ).send_keys(account[ 0 ]) browser.find_element_by_id( "TANGRAM__PSP_3__password" ).send_keys(account[ 1 ]) # 點(diǎn)擊登陸登陸 # id="TANGRAM__PSP_3__submit" browser.find_element_by_id( "TANGRAM__PSP_3__submit" ).click() # 等待登陸10秒 # print('等待登陸10秒...') # time.sleep(10) print ( "等待網(wǎng)址加載完畢..." ) select = input ( "請觀察瀏覽器網(wǎng)站是否已經(jīng)登陸(y/n):" ) while 1 : if select = = "y" or select = = "Y" : print ( "登陸成功!" ) print ( "準(zhǔn)備打開新的窗口..." ) # time.sleep(1) # browser.quit() break elif select = = "n" or select = = "N" : selectno = input ( "賬號密碼錯誤請按0,驗證碼出現(xiàn)請按1..." ) # 賬號密碼錯誤則重新輸入 if selectno = = "0" : # 找到id="TANGRAM__PSP_3__userName"的對話框 # 清空輸入框 browser.find_element_by_id( "TANGRAM__PSP_3__userName" ).clear() browser.find_element_by_id( "TANGRAM__PSP_3__password" ).clear() # 輸入賬號密碼 account = [] try : fileaccount = open ( "../baidu/account.txt" ) accounts = fileaccount.readlines() for acc in accounts: account.append(acc.strip()) fileaccount.close() except Exception as err: print (err) input ( "請正確在account.txt里面寫入賬號密碼" ) exit() browser.find_element_by_id( "TANGRAM__PSP_3__userName" ).send_keys(account[ 0 ]) browser.find_element_by_id( "TANGRAM__PSP_3__password" ).send_keys(account[ 1 ]) # 點(diǎn)擊登陸sign in # id="TANGRAM__PSP_3__submit" browser.find_element_by_id( "TANGRAM__PSP_3__submit" ).click() elif selectno = = "1" : # 驗證碼的id為id="ap_captcha_guess"的對話框 input ( "請在瀏覽器中輸入驗證碼并登陸..." ) select = input ( "請觀察瀏覽器網(wǎng)站是否已經(jīng)登陸(y/n):" ) else : print ( "請輸入“y”或者“n”!" ) select = input ( "請觀察瀏覽器網(wǎng)站是否已經(jīng)登陸(y/n):" ) |
登陸的頁面:
登陸過后需要打開新的窗口,也就是打開百度指數(shù),并且切換窗口,在selenium用:
1
2
3
4
5
6
7
8
9
10
|
# 新開一個窗口,通過執(zhí)行js來新開一個窗口 js = 'window.open("http://index.baidu.com");' browser.execute_script(js) # 新窗口句柄切換,進(jìn)入百度指數(shù) # 獲得當(dāng)前打開所有窗口的句柄handles # handles為一個數(shù)組 handles = browser.window_handles # print(handles) # 切換到當(dāng)前最新打開的窗口 browser.switch_to_window(handles[ - 1 ]) |
清空輸入框,構(gòu)造點(diǎn)擊天數(shù):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# 清空輸入框 browser.find_element_by_id( "schword" ).clear() # 寫入需要搜索的百度指數(shù) browser.find_element_by_id( "schword" ).send_keys(keyword) # 點(diǎn)擊搜索 # <input type="submit" value="" id="searchWords" onclick="searchDemoWords()"> browser.find_element_by_id( "searchWords" ).click() time.sleep( 2 ) # 最大化窗口 browser.maximize_window() # 構(gòu)造天數(shù) sel = int ( input ( "查詢7天請按0,30天請按1,90天請按2,半年請按3:" )) day = 0 if sel = = 0 : day = 7 elif sel = = 1 : day = 30 elif sel = = 2 : day = 90 elif sel = = 3 : day = 180 sel = '//a[@rel="' + str (day) + '"]' browser.find_element_by_xpath(sel).click() # 太快了 time.sleep( 2 ) |
天數(shù)也就是這里:
找到圖形框:
1
|
xoyelement = browser.find_elements_by_css_selector( "#trend rect" )[ 2 ] |
圖形框就是:
根據(jù)坐標(biāo)點(diǎn)的不同構(gòu)造偏移量:
選取7天的坐標(biāo)來觀察:
第一個點(diǎn)的橫坐標(biāo)為1031.66666
第二個點(diǎn)的橫坐標(biāo)為1234
所以7天兩個坐標(biāo)之間的差為:202.33,其他的天數(shù)類似
用selenium庫來模擬鼠標(biāo)滑動懸浮:
1
2
|
from selenium.webdriver.common.action_chains import ActionChains ActionChains(browser).move_to_element_with_offset(xoyelement,x_0,y_0).perform() |
但是這樣子確定的點(diǎn)指出是在這個位置:
也就是矩形的左上角,這里是不會加載js顯示彈出框的,所以要給橫坐標(biāo)+1:
1
2
|
x_0 = 1 y_0 = 0 |
寫個按照天數(shù)的循環(huán),讓橫坐標(biāo)累加:
1
2
3
4
5
6
7
8
9
10
11
|
# 按照選擇的天數(shù)循環(huán) for i in range (day): # 構(gòu)造規(guī)則 if day = = 7 : x_0 = x_0 + 202.33 elif day = = 30 : x_0 = x_0 + 41.68 elif day = = 90 : x_0 = x_0 + 13.64 elif day = = 180 : x_0 = x_0 + 6.78 |
鼠標(biāo)橫移時會彈出框,在網(wǎng)址里面找到這個框:
selenium自動識別之...:
1
2
|
# <div class="imgtxt" style="margin-left:-117px;"></div> imgelement = browser.find_element_by_xpath( '//div[@id="viewbox"]' ) |
并且確定這個框的大小位置:
1
2
3
4
5
6
7
8
9
|
# 找到圖片坐標(biāo) locations = imgelement.location print (locations) # 找到圖片大小 sizes = imgelement.size print (sizes) # 構(gòu)造指數(shù)的位置 rangle = ( int (locations[ 'x' ]), int (locations[ 'y' ]), int (locations[ 'x' ] + sizes[ 'width' ]), int (locations[ 'y' ] + sizes[ 'height' ])) |
截取的圖形為:
下面的思路就是:
1.將整個屏幕截圖下來
2.打開截圖用上面得到的這個坐標(biāo)rangle進(jìn)行裁剪
但是最后裁剪出來的是上面的那個黑框,我想要的效果是:
所以要對rangle進(jìn)行計算,但是我懶,忽略了搜索詞的長度,直接暴力的寫成:
1
2
3
|
# 構(gòu)造指數(shù)的位置 rangle = ( int (locations[ 'x' ] + sizes[ 'width' ] / 3 ), int (locations[ 'y' ] + sizes[ 'height' ] / 2 ), int (locations[ 'x' ] + sizes[ 'width' ] * 2 / 3 ), int (locations[ 'y' ] + sizes[ 'height' ])) |
這個寫法最終不太好,最起碼要對keyword的長度進(jìn)行判斷,長度過長會導(dǎo)致截圖坐標(biāo)出現(xiàn)偏差,反正我知道怎么做,就是不寫出來給你們看!
后面的完整代碼是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# <div class="imgtxt" style="margin-left:-117px;"></div> imgelement = browser.find_element_by_xpath( '//div[@id="viewbox"]' ) # 找到圖片坐標(biāo) locations = imgelement.location print (locations) # 找到圖片大小 sizes = imgelement.size print (sizes) # 構(gòu)造指數(shù)的位置 rangle = ( int (locations[ 'x' ] + sizes[ 'width' ] / 3 ), int (locations[ 'y' ] + sizes[ 'height' ] / 2 ), int (locations[ 'x' ] + sizes[ 'width' ] * 2 / 3 ), int (locations[ 'y' ] + sizes[ 'height' ])) # 截取當(dāng)前瀏覽器 path = "../baidu/" + str (num) browser.save_screenshot( str (path) + ".png" ) # 打開截圖切割 img = Image. open ( str (path) + ".png" ) jpg = img.crop(rangle) jpg.save( str (path) + ".jpg" ) |
但是后面發(fā)現(xiàn)裁剪的圖片太小,識別精度太低,所以需要對圖片進(jìn)行擴(kuò)大:
1
2
3
4
5
6
7
8
|
# 將圖片放大一倍 # 原圖大小73.29 jpgzoom = Image. open ( str (path) + ".jpg" ) (x, y) = jpgzoom.size x_s = 146 y_s = 58 out = jpgzoom.resize((x_s, y_s), Image.ANTIALIAS) out.save(path + 'zoom.jpg' , 'png' , quality = 95 ) |
原圖大小請 右鍵->屬性->詳細(xì)信息 查看,我的是長73像素,寬29像素
最后就是圖像識別
1
2
3
4
5
6
|
# 圖像識別 index = [] image = Image. open ( str (path) + "zoom.jpg" ) code = pytesseract.image_to_string(image) if code: index.append(code) |
最后效果圖:
源碼下載:demo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/TTyb/p/6051366.html