百度指數抓取,再用圖像識別得到指數
前言:
土福曾說,百度指數很難抓,在淘寶上面是20塊1個關鍵字:

哥那么叼的人怎么會被他嚇到,于是乎花了零零碎碎加起來大約2天半搞定,在此鄙視一下土福
安裝的庫很多:
谷歌圖像識別tesseract-ocrpip3 install pillowpip3 install pyocrselenium2.45Chrome47.0.2526.106 m or Firebox32.0.1chromedriver.exe
進入百度指數需要登陸,登陸的賬號密碼寫在文本account里面:

萬能登陸代碼如下:
# 打開瀏覽器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() # 輸入網址 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]) # 點擊登陸登陸 # id="TANGRAM__PSP_3__submit" browser.find_element_by_id("TANGRAM__PSP_3__submit").click() # 等待登陸10秒 # print('等待登陸10秒...') # time.sleep(10) print("等待網址加載完畢...") select = input("請觀察瀏覽器網站是否已經登陸(y/n):") while 1: if select == "y" or select == "Y": print("登陸成功!") print("準備打開新的窗口...") # time.sleep(1) # browser.quit() break elif select == "n" or select == "N": selectno = input("賬號密碼錯誤請按0,驗證碼出現請按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]) # 點擊登陸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("請觀察瀏覽器網站是否已經登陸(y/n):") else: print("請輸入“y”或者“n”!") select = input("請觀察瀏覽器網站是否已經登陸(y/n):")登陸的頁面:

登陸過后需要打開新的窗口,也就是打開百度指數,并且切換窗口,在selenium用:
# 新開一個窗口,通過執行js來新開一個窗口js = 'window.open("http://index.baidu.com");'browser.execute_script(js)# 新窗口句柄切換,進入百度指數# 獲得當前打開所有窗口的句柄handles# handles為一個數組handles = browser.window_handles# print(handles)# 切換到當前最新打開的窗口browser.switch_to_window(handles[-1])清空輸入框,構造點擊天數:
# 清空輸入框browser.find_element_by_id("schword").clear()# 寫入需要搜索的百度指數browser.find_element_by_id("schword").send_keys(keyword)# 點擊搜索# <input type="submit" value="" id="searchWords" onclick="searchDemoWords()">browser.find_element_by_id("searchWords").click()time.sleep(2)# 最大化窗口browser.maximize_window()# 構造天數sel = int(input("查詢7天請按0,30天請按1,90天請按2,半年請按3:"))day = 0if sel == 0: day = 7elif sel == 1: day = 30elif sel == 2: day = 90elif sel == 3: day = 180sel = '//a[@rel="' + str(day) + '"]'browser.find_element_by_xpath(sel).click()# 太快了time.sleep(2)天數也就是這里:

找到圖形框:
xoyelement = browser.find_elements_by_css_selector("#trend rect")[2]圖形框就是:

根據坐標點的不同構造偏移量:

選取7天的坐標來觀察:
第一個點的橫坐標為1031.66666
第二個點的橫坐標為1234

所以7天兩個坐標之間的差為:202.33,其他的天數類似
用selenium庫來模擬鼠標滑動懸浮:
from selenium.webdriver.common.action_chains import ActionChainsActionChains(browser).move_to_element_with_offset(xoyelement,x_0,y_0).perform()
但是這樣子確定的點指出是在這個位置:

也就是矩形的左上角,這里是不會加載js顯示彈出框的,所以要給橫坐標+1:
x_0 = 1y_0 = 0
寫個按照天數的循環,讓橫坐標累加:
# 按照選擇的天數循環for i in range(day): # 構造規則 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
鼠標橫移時會彈出框,在網址里面找到這個框:

selenium自動識別之...:
# <div class="imgtxt" style="margin-left:-117px;"></div>imgelement = browser.find_element_by_xpath('//div[@id="viewbox"]')并且確定這個框的大小位置:
# 找到圖片坐標locations = imgelement.locationprint(locations)# 找到圖片大小sizes = imgelement.sizeprint(sizes)# 構造指數的位置rangle = (int(locations['x']), int(locations['y']), int(locations['x'] + sizes['width']), int(locations['y'] + sizes['height']))
截取的圖形為:

下面的思路就是:
1.將整個屏幕截圖下來
2.打開截圖用上面得到的這個坐標rangle進行裁剪
但是最后裁剪出來的是上面的那個黑框,我想要的效果是:

所以要對rangle進行計算,但是我懶,忽略了搜索詞的長度,直接暴力的寫成:
# 構造指數的位置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的長度進行判斷,長度過長會導致截圖坐標出現偏差,反正我知道怎么做,就是不寫出來給你們看!
后面的完整代碼是:
# <div class="imgtxt" style="margin-left:-117px;"></div>imgelement = browser.find_element_by_xpath('//div[@id="viewbox"]')# 找到圖片坐標locations = imgelement.locationprint(locations)# 找到圖片大小sizes = imgelement.sizeprint(sizes)# 構造指數的位置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']))# 截取當前瀏覽器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")但是后面發現裁剪的圖片太小,識別精度太低,所以需要對圖片進行擴大:
# 將圖片放大一倍# 原圖大小73.29jpgzoom = Image.open(str(path) + ".jpg")(x, y) = jpgzoom.sizex_s = 146y_s = 58out = jpgzoom.resize((x_s, y_s), Image.ANTIALIAS)out.save(path + 'zoom.jpg', 'png', quality=95)
原圖大小請 右鍵->屬性->詳細信息 查看,我的是長73像素,寬29像素
最后就是圖像識別
# 圖像識別index = []image = Image.open(str(path) + "zoom.jpg")code = pytesseract.image_to_string(image)if code: index.append(code)
最后效果圖:


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答