2018.11.6 更新 跳转链接
百度指数 返回的数据不好获取,这里就使用selenium 加 图像识别
1.需要获取数据的话,必须先登录百度,一个账号一天最多请求300次,PC端和手机端次数是分开的
2.获取数据 需要使用selenium 截图 ,然后使用图像识别来进行识别数字就行,然后我们将识别出来的数据保持
简单尝试的代码,如果需要大批量获取数据,就需要很多贴吧账号,redis保持cookie,ip代理,处理验证码,等等
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from PIL import Image
import pytesseract
options = webdriver.ChromeOptions()
# 设置中文
options.add_argument('lang=zh_CN.UTF-8')
# 更换头部
options.add_argument(
'user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"')
browser = webdriver.Chrome("E:\guge\Google\Chrome\Application\chromedriver.exe", chrome_options=options)
# 打开浏览器
def openbrowser():
# 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.get(url)
# 打开浏览器时间
# print("等待10秒打开浏览器...")
# time.sleep(10)
# 找到id="TANGRAM__PSP_3__userName"的对话框
# 清空输入框
# 点击用户名输入
# browser.find_element_by_id("TANGRAM__PSP_3__footerULoginBtn").clear()
#
# 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", encoding='UTF-8')
accounts = fileaccount.readlines()
for acc in accounts:
account.append(acc.strip())
fileaccount.close()
except Exception as err:
print(err)
input("请正确在account.txt里面写入账号密码")
exit()
print(account[0])
print(account[1])
time.sleep(2)
browser.find_element_by_id("TANGRAM__PSP_3__footerULoginBtn").click()