详解python3百度指数抓取实例


Posted in Python onDecember 12, 2016

百度指数抓取,再用图像识别得到指数

前言:

土福曾说,百度指数很难抓,在淘宝上面是20块1个关键字:

详解python3百度指数抓取实例

哥那么叼的人怎么会被他吓到,于是乎花了零零碎碎加起来大约2天半搞定,在此鄙视一下土福

安装的库很多:

谷歌图像识别tesseract-ocr

pip3 install pillow

pip3 install pyocr

selenium2.45

Chrome47.0.2526.106 m or Firebox32.0.1

chromedriver.exe

图像识别验证码请参考:https://3water.com/article/92287.htm

selenium用法请参考:https://3water.com/article/52329.htm

进入百度指数需要登陆,登陆的账号密码写在文本account里面:

详解python3百度指数抓取实例

万能登陆代码如下:

# 打开浏览器
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):")

登陆的页面:

详解python3百度指数抓取实例

登陆过后需要打开新的窗口,也就是打开百度指数,并且切换窗口,在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 = 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)

天数也就是这里:

详解python3百度指数抓取实例

找到图形框:

xoyelement = browser.find_elements_by_css_selector("#trend rect")[2]

图形框就是:

详解python3百度指数抓取实例

根据坐标点的不同构造偏移量:

详解python3百度指数抓取实例

选取7天的坐标来观察:

第一个点的横坐标为1031.66666

第二个点的横坐标为1234

详解python3百度指数抓取实例

所以7天两个坐标之间的差为:202.33,其他的天数类似

用selenium库来模拟鼠标滑动悬浮:

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(browser).move_to_element_with_offset(xoyelement,x_0,y_0).perform()

但是这样子确定的点指出是在这个位置:

详解python3百度指数抓取实例

也就是矩形的左上角,这里是不会加载js显示弹出框的,所以要给横坐标+1:

x_0 = 1
y_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

鼠标横移时会弹出框,在网址里面找到这个框:

详解python3百度指数抓取实例

selenium自动识别之...:

# <div class="imgtxt" style="margin-left:-117px;"></div>
imgelement = browser.find_element_by_xpath('//div[@id="viewbox"]')

并且确定这个框的大小位置:

# 找到图片坐标
locations = imgelement.location
print(locations)
# 找到图片大小
sizes = imgelement.size
print(sizes)
# 构造指数的位置
rangle = (int(locations['x']), int(locations['y']), int(locations['x'] + sizes['width']),
     int(locations['y'] + sizes['height']))

截取的图形为:

详解python3百度指数抓取实例

下面的思路就是:

1.将整个屏幕截图下来

2.打开截图用上面得到的这个坐标rangle进行裁剪

但是最后裁剪出来的是上面的那个黑框,我想要的效果是:

 详解python3百度指数抓取实例

所以要对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.location
print(locations)
# 找到图片大小
sizes = imgelement.size
print(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.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)

原图大小请 右键->属性->详细信息 查看,我的是长73像素,宽29像素

最后就是图像识别

# 图像识别
index = []
image = Image.open(str(path) + "zoom.jpg")
code = pytesseract.image_to_string(image)
if code:
  index.append(code)

最后效果图:

详解python3百度指数抓取实例

详解python3百度指数抓取实例

源码下载:demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python程序员开发中常犯的10个错误
Jul 07 Python
Python中的面向对象编程详解(下)
Apr 13 Python
听歌识曲--用python实现一个音乐检索器的功能
Nov 15 Python
几种实用的pythonic语法实例代码
Feb 24 Python
Python爬虫之正则表达式的使用教程详解
Oct 25 Python
Python3.5面向对象程序设计之类的继承和多态详解
Apr 24 Python
python实现拼图小游戏
Feb 22 Python
python3注册全局热键的实现
Mar 22 Python
Visual Studio Code搭建django项目的方法步骤
Sep 17 Python
Python爬虫模拟登陆哔哩哔哩(bilibili)并突破点选验证码功能
Dec 21 Python
Python快速优雅的批量修改Word文档样式
May 20 Python
详解Python中__new__方法的作用
Mar 31 Python
python实现多线程抓取知乎用户
Dec 12 #Python
浅谈Python类里的__init__方法函数,Python类的构造函数
Dec 10 #Python
详解常用查找数据结构及算法(Python实现)
Dec 09 #Python
详解Python装饰器由浅入深
Dec 09 #Python
python利用正则表达式提取字符串
Dec 08 #Python
基于python的七种经典排序算法(推荐)
Dec 08 #Python
Python序列操作之进阶篇
Dec 08 #Python
You might like
用文本文件制作留言板提示(上)
2006/10/09 PHP
php地址引用(php地址引用的效率问题)
2012/03/23 PHP
php中防止恶意刷新页面的代码小结
2012/10/31 PHP
浅析php静态方法与非静态方法的用法区别
2016/05/17 PHP
解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]
2020/04/06 PHP
javascript 获取select下拉列表值的代码
2009/09/07 Javascript
javascript对象之内置对象Math使用方法
2010/04/16 Javascript
jqPlot 图表中文API使用文档及源码和在线示例
2012/02/07 Javascript
js如何判断用户是否是用微信浏览器
2014/06/05 Javascript
javascript动态控制服务器控件实例
2014/09/05 Javascript
JS制作图形验证码实现代码
2020/10/19 Javascript
基于jQuery实现瀑布流页面
2017/04/11 jQuery
利用JS hash制作单页Web应用的方法详解
2017/10/10 Javascript
jQuery实现切换隐藏与显示同时切换图标功能
2017/10/29 jQuery
JavaScript类的继承方法小结【组合继承分析】
2018/07/11 Javascript
重学JS 系列:聊聊继承(推荐)
2019/04/11 Javascript
Vue项目中如何使用Axios封装http请求详解
2019/10/23 Javascript
jQuery实现中奖播报功能(让文本滚动起来) 简单设置数值即可
2020/03/20 jQuery
python 字典(dict)按键和值排序
2016/06/28 Python
Python图像处理之图像的缩放、旋转与翻转实现方法示例
2019/01/04 Python
基于python生成器封装的协程类
2019/03/20 Python
使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
2020/03/16 Python
Python爬虫抓取论坛关键字过程解析
2020/10/19 Python
HTML5 通过Vedio标签实现视频循环播放的示例代码
2020/08/05 HTML / CSS
台湾菁英交友:结识黄金单身的台湾人
2018/01/22 全球购物
墨西哥皇宫度假村预订:Palace Resorts
2018/06/16 全球购物
一套Delphi的笔试题一
2016/02/14 面试题
技校生自我鉴定范文
2013/09/26 职场文书
小学三年级数学教学反思
2014/01/31 职场文书
继承权公证书
2014/04/09 职场文书
工伤事故赔偿协议书
2014/10/27 职场文书
2016新党章学习心得体会
2016/01/15 职场文书
Python多个MP4合成视频的实现方法
2021/07/16 Python
Python实现视频中添加音频工具详解
2021/12/06 Python
Centos系统通过Docker安装并搭建MongoDB数据库
2022/04/12 MongoDB
MySQL 字符集 character
2022/05/04 MySQL