百度指数抓取,再用图像识别得到指数
前言:
土福曾说,百度指数很难抓,在淘宝上面是20块1个关键字:

哥那么叼的人怎么会被他吓到,于是乎花了零零碎碎加起来大约2天半搞定,在此鄙视一下土福
安装的库很多:
谷歌图像识别tesseract-ocr
pip3 install pillow
pip3 install pyocr
selenium2.45
Chrome47.0.2526.106 m or Firebox32.0.1
chromedriver.exe
图像识别验证码请参考:https://www.jb51.net/article/92287.htm
selenium用法请参考:https://www.jb51.net/article/52329.htm
进入百度指数需要登陆,登陆的账号密码写在文本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_elementCBHK\J
WK[XYSPSPTBJ]
K ]X[]ONMJCBOB]Bc)c+I)k"$y+(;kLy`(B 9d#l,y+*BB]\HHBH\H\NBf*[^HB[XYH[XY[]
H
KCBHH]\\X[XY[[XYCBYNB[^\[
JCBOB]B 9d#b99f&B[Y[HZZ[Z[[^][KNKLL
LLXLLNYKB[Y[HZZ[Z[[^][KNKXMYY
XMLXXXMB9."&HYHLX^ZKLK]MLX[XKИZYZ[^[X\\LK\\H[[[BB."l,y+j:`yk{n#9&)9ki.h9"y`9n+b{.g&i&i&+/c. |