詳解python3百度指數(shù)抓取實(shí)例
百度指數(shù)抓取,再用圖像識別得到指數(shù)
前言:
土福曾說,百度指數(shù)很難抓,在淘寶上面是20塊1個(gè)關(guān)鍵字:
哥那么叼的人怎么會被他嚇到,于是乎花了零零碎碎加起來大約2天半搞定,在此鄙視一下土福
安裝的庫很多:
谷歌圖像識別tesseract-ocr pip3 install pillow pip3 install pyocr selenium2.45 Chrome47.0.2526.106 m or Firebox32.0.1 chromedriver.exe
圖像識別驗(yàn)證碼請參考:http://chabaoo.cn/article/92287.htm
selenium用法請參考:http://chabaoo.cn/article/52329.htm
進(jìn)入百度指數(shù)需要登陸,登陸的賬號密碼寫在文本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() # 輸入網(wǎng)址 browser.get(url) # 打開瀏覽器時(shí)間 # 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("賬號密碼錯(cuò)誤請按0,驗(yàn)證碼出現(xiàn)請按1...") # 賬號密碼錯(cuò)誤則重新輸入 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": # 驗(yàn)證碼的id為id="ap_captcha_guess"的對話框 input("請?jiān)跒g覽器中輸入驗(yàn)證碼并登陸...") select = input("請觀察瀏覽器網(wǎng)站是否已經(jīng)登陸(y/n):") else: print("請輸入“y”或者“n”!") select = input("請觀察瀏覽器網(wǎng)站是否已經(jīng)登陸(y/n):")
登陸的頁面:
登陸過后需要打開新的窗口,也就是打開百度指數(shù),并且切換窗口,在selenium用:
# 新開一個(gè)窗口,通過執(zhí)行js來新開一個(gè)窗口 js = 'window.open("http://index.baidu.com");' browser.execute_script(js) # 新窗口句柄切換,進(jìn)入百度指數(shù) # 獲得當(dāng)前打開所有窗口的句柄handles # handles為一個(gè)數(shù)組 handles = browser.window_handles # print(handles) # 切換到當(dāng)前最新打開的窗口 browser.switch_to_window(handles[-1])
清空輸入框,構(gòu)造點(diǎn)擊天數(shù):
# 清空輸入框 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ù)也就是這里:
找到圖形框:
xoyelement = browser.find_elements_by_css_selector("#trend rect")[2]
圖形框就是:
根據(jù)坐標(biāo)點(diǎn)的不同構(gòu)造偏移量:
選取7天的坐標(biāo)來觀察:
第一個(gè)點(diǎn)的橫坐標(biāo)為1031.66666
第二個(gè)點(diǎn)的橫坐標(biāo)為1234
所以7天兩個(gè)坐標(biāo)之間的差為:202.33,其他的天數(shù)類似
用selenium庫來模擬鼠標(biāo)滑動懸?。?br />
from selenium.webdriver.common.action_chains import ActionChains ActionChains(browser).move_to_element_with_offset(xoyelement,x_0,y_0).perform()
但是這樣子確定的點(diǎn)指出是在這個(gè)位置:
也就是矩形的左上角,這里是不會加載js顯示彈出框的,所以要給橫坐標(biāo)+1:
x_0 = 1 y_0 = 0
寫個(gè)按照天數(shù)的循環(huán),讓橫坐標(biāo)累加:
# 按照選擇的天數(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)橫移時(shí)會彈出框,在網(wǎng)址里面找到這個(gè)框:
selenium自動識別之...:
# <div class="imgtxt" style="margin-left:-117px;"></div> imgelement = browser.find_element_by_xpath('//div[@id="viewbox"]')
并且確定這個(gè)框的大小位置:
# 找到圖片坐標(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.將整個(gè)屏幕截圖下來
2.打開截圖用上面得到的這個(gè)坐標(biāo)rangle進(jìn)行裁剪
但是最后裁剪出來的是上面的那個(gè)黑框,我想要的效果是:
所以要對rangle進(jìn)行計(jì)算,但是我懶,忽略了搜索詞的長度,直接暴力的寫成:
# 構(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']))
這個(gè)寫法最終不太好,最起碼要對keyword的長度進(jìn)行判斷,長度過長會導(dǎo)致截圖坐標(biāo)出現(xiàn)偏差,反正我知道怎么做,就是不寫出來給你們看!
后面的完整代碼是:
# <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ò)大:
# 將圖片放大一倍 # 原圖大小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像素
最后就是圖像識別
# 圖像識別 index = [] image = Image.open(str(path) + "zoom.jpg") code = pytesseract.image_to_string(image) if code: index.append(code)
最后效果圖:
源碼下載:demo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Python3分析sitemap.xml并抓取導(dǎo)出全站鏈接詳解
- python3實(shí)現(xiàn)抓取網(wǎng)頁資源的 N 種方法
- Python3使用requests包抓取并保存網(wǎng)頁源碼的方法
- 使用Python3編寫抓取網(wǎng)頁和只抓網(wǎng)頁圖片的腳本
- python3抓取中文網(wǎng)頁的方法
- 在Python3中使用asyncio庫進(jìn)行快速數(shù)據(jù)抓取的教程
- Python使用lxml模塊和Requests模塊抓取HTML頁面的教程
- 用Python程序抓取網(wǎng)頁的HTML信息的一個(gè)小實(shí)例
- python抓取并保存html頁面時(shí)亂碼問題的解決方法
- Python使用urllib2模塊抓取HTML頁面資源的實(shí)例分享
- Python3實(shí)現(xiàn)抓取javascript動態(tài)生成的html網(wǎng)頁功能示例
相關(guān)文章
使用Pytorch訓(xùn)練分類問題時(shí),分類準(zhǔn)確率的計(jì)算方式
這篇文章主要介紹了使用Pytorch訓(xùn)練分類問題時(shí),分類準(zhǔn)確率的計(jì)算方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09python3美化表格數(shù)據(jù)輸出結(jié)果的實(shí)現(xiàn)代碼
本文介紹了兩種表格數(shù)據(jù)的打印工具:tabulate和prettytable的安裝與基本使用方法,通過實(shí)例講解的非常詳細(xì),需要的朋友參考下吧2021-04-04Python圖像處理之識別圖像中的文字(實(shí)例講解)
今天小編就為大家分享一篇Python圖像處理之識別圖像中的文字(實(shí)例講解),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Python 處理數(shù)據(jù)的實(shí)例詳解
這篇文章主要介紹了Python 處理數(shù)據(jù)的實(shí)例詳解的相關(guān)資料,這里主要介紹Python 常用的基礎(chǔ)知識并附實(shí)例,需要的朋友可以參考下2017-08-08