• DAY 90 爬虫04


    1 selenium模拟人的行为
    find_elements_by_xx
       find_elements_by_css_select('css选择器')
       send_keys
       click
       clear
    2 无界面浏览器
    -配置
    3 其他使用
    -标签位置,标签大小,标签属性
       -模拟浏览器前进后台
       -tab切换
       -获取cookie
      -driver.get_cookies()--->列表
           
    4 代理池和cookie池的使用

    5 抽屉自动点赞
    -使用selenium登录,使用requests发请求
       
    9 爬取京东商品信息
    -img懒加载
       -点击下一页

     

    1 动作链和切换frame(了解)

    # 切换frame
    driver.switch_to.frame('id')
    #动作链
    ActionChains(driver).drag_and_drop(sourse,target).perform()
    ActionChains(driver).drag_and_drop_by_offset(sourse,10,0).perform()

    # 鼠标点住,移动,松开鼠标
    ActionChains(driver).click_and_hold(sourse).perform()
    ActionChains(driver).move_by_offset(xoffset=20,yoffset=0).perform()
    ActionChains(driver).release().perform()

     

    2 打码平台的使用

    1 别的平台帮助我们失败验证码,我们只需要花点钱
    2 云打码,超级鹰
    3 注册用户---》充钱
    4 看一下价格体系---》识别不同验证码价格不一样
    5 下载demo,测试demo
    #!/usr/bin/env python
    # coding:utf-8

    import requests
    from hashlib import md5


    class Chaojiying_Client(object):

       def __init__(self, username, password, soft_id):
           self.username = username
           password = password.encode('utf8')
           self.password = md5(password).hexdigest()
           self.soft_id = soft_id
           self.base_params = {
               'user': self.username,
               'pass2': self.password,
               'softid': self.soft_id,
          }
           self.headers = {
               'Connection': 'Keep-Alive',
               'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
          }

       def PostPic(self, im, codetype):
           """
          im: 图片字节
          codetype: 题目类型 参考 http://www.chaojiying.com/price.html
          """
           params = {
               'codetype': codetype,
          }
           params.update(self.base_params)
           files = {'userfile': ('ccc.jpg', im)}
           r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files,
                             headers=self.headers)
           return r.json()

       def ReportError(self, im_id):
           """
          im_id:报错题目的图片ID
          """
           params = {
               'id': im_id,
          }
           params.update(self.base_params)
           r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
           return r.json()


    if __name__ == '__main__':
       chaojiying = Chaojiying_Client('', '', '903641')  # 用户中心>>软件ID 生成一个替换 96001
       im = open('code.png', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
       print(chaojiying.PostPic(im, 1004))  # 1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()

     

    3 xpath的使用

    1 一门在html中查找数据的语言
    2 记住的语法:
    /   取当前路径下的xx
       //  取所有路径下的xx
      .   当前路径
      ..   上一层
    @    取属性
       
    4 lxml解析模块提供的xpath


    doc='''
    <html>
    <head>
    <base href='http://example.com/' />
    <title>Example website</title>
    </head>
    <body>
    <div id='images'>
      <a href='image1.html' name='sss'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
      <a href='image2.html' name='lqz'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
      <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
      <a href='image4.html' class='li'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
      <a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
      <a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
    </div>
    </body>
    </html>
    '''
    from lxml import etree

    # 传入要解析的内容
    html=etree.HTML(doc)

    # res=html.xpath('//body')
    # print(res)

    # 1 所有节点
    # a=html.xpath('//*')




    # 2 指定节点(结果为列表)
    # a=html.xpath('//head')
    # 3 子节点,子孙节点
    # a=html.xpath('//div/a')
    # a=html.xpath('//body/a') #无数据
    # a=html.xpath('//body//a')
    # 4 父节点
    # a=html.xpath('//body//a[@href="image1.html"]/..')
    # a=html.xpath('//body//a[@href="image1.html"]')
    # a=html.xpath('//body//a[1]/..')
    # 也可以这样
    # a=html.xpath('//body//a[1]/parent::*')
    # 5 属性匹配
    # a=html.xpath('//a[@href="image1.html"]')

    # 6 文本获取 text()
    # a=html.xpath('//body//a[@href="image1.html"]/text()')
    # a=html.xpath('//a/text()')

    # 7 属性获取
    # a=html.xpath('//body//a/@href')
    # # 注意从1 开始取(不是从0)
    # a=html.xpath('//body//a[2]/@href')
    # 8 属性多值匹配
    # a 标签有多个class类,直接匹配就不可以了,需要用contains
    # a=html.xpath('//a[@class="li"]')
    # a=html.xpath('//body//a[contains(@class,"li")]')
    # a=html.xpath('//body//a[contains(@class,"li")]/text()')
    # 9 多属性匹配
    # a=html.xpath('//body//a[contains(@class,"li") or @name="items"]')
    # a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
    # a=html.xpath('//body//a[contains(@class,"li")]/text()')
    # 10 按序选择
    # a=html.xpath('//a[2]/text()')
    # a=html.xpath('//a[2]/@href')
    # a=html.xpath('//a[2]/@name')
    # 取最后一个
    # a=html.xpath('//a[last()]/@href')
    # 位置小于3的
    # a=html.xpath('//a[position()<3]/@href')
    # 倒数第二个
    # a=html.xpath('//a[last()-2]/@href')
    # 11 节点轴选择
    # ancestor:祖先节点
    # 使用了* 获取所有祖先节点
    # a=html.xpath('//a/ancestor::*')
    # # 获取祖先节点中的div
    # a=html.xpath('//a/ancestor::div')
    # attribute:属性值
    # a=html.xpath('//a[1]/attribute::*')
    # child:直接子节点
    # a=html.xpath('//a[1]/child::*')
    # a=html.xpath('//a[1]/child::img')
    # descendant:所有子孙节点
    # a=html.xpath('//a[6]/descendant::*')
    # following:当前节点之后所有节点
    # a=html.xpath('//a[1]/following::*')
    # a=html.xpath('//a[1]/following::*[1]/@href')
    # following-sibling:当前节点之后同级节点
    # a=html.xpath('//a[1]/following-sibling::*')
    # a=html.xpath('//a[1]/following-sibling::a')
    # a=html.xpath('//a[1]/following-sibling::*[2]/text()')
    a=html.xpath('//a[1]/following-sibling::*[2]/@href')

    print(a)

     

    4 自动登录12306

    1 打开连接:https://kyfw.12306.cn/otn/resources/login.html
    2 点击账号登录
    3 找出用户名,密码框,输入正确的用户名密码
    4 扣除验证码
    -方案一:截屏幕,取到验证码的位置和大小,pillow取大图中扣除验证码
    -方案二:验证码图是base64编码,把编码转成图片
    5 超级鹰验证
    6 使用动作链点击坐标
    7 点击登录
    8 滑动验证
    9 进入

     


    from selenium import webdriver
    import time
    from selenium.webdriver import ActionChains
    import base64
    from chaojiying import Chaojiying_Client
    from PIL import Image

    # 不让程序检测出是用驱动控制
    from selenium.webdriver.chrome.options import Options
    options = Options()
    options.add_argument("--disable-blink-features=AutomationControlled")
    driver=webdriver.Chrome(executable_path='./chromedriver.exe',chrome_options=options)

    # driver=webdriver.Chrome(executable_path='chromedriver.exe')
    driver.implicitly_wait(10)
    driver.get('https://kyfw.12306.cn/otn/resources/login.html')
    # 把窗口设置全屏
    driver.maximize_window()
    try:
       # user_login=driver.find_element_by_css_selector('body > div.login-panel > div.login-box > ul > li.login-hd-account > a')
       user_login=driver.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a')
       # user_login=driver.find_element_by_link_text('账号登录')

       user_login.click()

       username=driver.find_element_by_id('J-userName')
       password=driver.find_element_by_id('J-password')

       username.send_keys('18953675224')
       password.send_keys('lqz12345')

       #保存当前屏幕截图
       driver.save_screenshot('main.png')
       j_login_img=driver.find_element_by_id('J-loginImg')
       # 方式一:使用抠图的方式
       # location=j_login_img.location
       # size=j_login_img.size
       # print(location)
       # print(size)
       # #验证码的坐标
       # img_tu = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
       # #使用pillow打开截图
       # img=Image.open('./main.png')
       # #从截图中按照位置扣除验证码
       # code_img=img.crop(img_tu)
       # # 把扣出来的图,保存到本地
       # code_img.save('./code.png')

       # 方式二:使用base64转成图片
       img_base64=j_login_img.get_attribute('src').split(',')[-1]
       # 把base64解码
       img_content=base64.b64decode(img_base64)
       with open('code1.png','wb') as f:
           f.write(img_content)

       # 调用超级鹰识别
       chaojiying = Chaojiying_Client('306334678', 'lqz123', '903641')  # 用户中心>>软件ID 生成一个替换 96001
       im = open('code1.png', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
       res=chaojiying.PostPic(im, 9004)  # 1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
       # 123,155|42,135
       print(res)
       result=res['pic_str']
       all_list = []
       if '|' in result:
           list_1 = result.split('|')
           count_1 = len(list_1)
           for i in range(count_1):
               xy_list = []
               x = int(list_1[i].split(',')[0])
               y = int(list_1[i].split(',')[1])
               xy_list.append(x)
               xy_list.append(y)
               all_list.append(xy_list)
       else:
           x = int(result.split(',')[0])
           y = int(result.split(',')[1])
           xy_list = []
           xy_list.append(x)
           xy_list.append(y)
           all_list.append(xy_list)
       print(all_list)

       #点击
       for item in all_list:
           ActionChains(driver).move_to_element_with_offset(j_login_img,item[0],item[1]).click().perform()
           time.sleep(1)

       login_btn=driver.find_element_by_id('J-login')
       time.sleep(5)
       login_btn.click()

       # 滑动验证码
       span=driver.find_element_by_id('nc_1_n1z')
       ActionChains(driver).drag_and_drop_by_offset(span,300,0).perform()
       time.sleep(10)

    except Exception as e:
       print(e)
    finally:
       driver.close()

    5 scrapy介绍,架构,安装

    1 批量,快递爬取
    2 使用框架,在固定位置写固定代码,速度很快
    3 爬虫界的django,用起来,跟djagno很像
    4 Scrapy 是基于twisted框架开发而来,twisted是一个流行的事件驱动的python网络框架
    5 可扩展性很高


    6 scrapy架构
    -爬虫 spiders
    -咱们在这里写爬虫代码(爬哪些网址)
    -解析数据(自己写)
    -爬虫中间件
    -用的比较少
    -拦截
    -引擎 :大总管,控制数据的流动
    -调度器:负责调度要爬取的请求(先进先出,先进后出,优先级)
    -同时去除重复的网址(集合,布隆过滤器)
    -深度优先:
    -摁着一条线爬取
    -先进先出
    -广度优先
    -后进先出
    -下载中间件(用的最多的)
    -拦截
    -加请求头
    -加代理,加cookie
    -集成selenium
    -下载器
    -真正的去下载的(twisted:事件驱动)
    -管道
    -存储数据(文件,redis,mysql)



    # 运气好
    pip3 install scrapy
    #运气不好
    1、pip3 install wheel #安装后,便支持通过wheel文件安装软件,wheel文件官网:https://www.lfd.uci.edu/~gohlke/pythonlibs
    3、pip3 install lxml
    4、pip3 install pyopenssl
    5、下载并安装pywin32:https://sourceforge.net/projects/pywin32/files/pywin32/
    6、下载twisted的wheel文件:http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
    7、执行pip3 install 下载目录Twisted-17.9.0-cp36-cp36m-win_amd64.whl
    8、pip3 install scrapy




    # windows上,如果有模块装不上,去网站下载对应的wheel文件,安装
    pip3 install D:DownloadsTwisted-20.3.0-cp36-cp36m-win_amd64.whl

     

    6 scrapy创建项目,创建爬虫

    1 创建项目(django中创建项目)
    scrapy startproject myfirstscrapy
    2 创建爬虫(django中创建app)
    scrapy genspider 爬虫名 地址
    scrapy genspider chouti dig.chouti.com


    3 其他命令
    -scrapy view http://www.taobao.com # 检测这个页面的书

     

    7 scrapy目录介绍

    myfirstscrapy  #项目名
    myfirstscrapy #文件夹
    __init__.py
    -spiders #文件夹,内部包含所有的爬虫,一个爬虫就是一个py文件
    -__init__.py
    -chouti.py #抽屉爬虫
    middlewares.py #爬虫中间件,下载中间件,都放在这里面
    items.py #类比models,一个个的类,后期类实例化存储数据
    settings.py #项目配置文件
    pipelines.py #管道,用来存储数据,这里写存数据的代码
    scrapy.cfg #项目上线会修改

    8 启动爬虫

    1 启动爬虫 
    scrapy crawl chouti
    scrapy crawl chouti --nolog # 不打印日志

    2 把配置文件中改一下(不遵循爬虫协议)
    ROBOTSTXT_OBEY = False

    3 点击邮件,使用pycharm直接运行
    在项目路径下新建一个main.py
    from scrapy.cmdline import execute
    #相当于在命令行中执行命令
    execute(['scrapy', 'crawl', 'chouti', '--nolog'])

    9 解析数据

    1 response对象有css方法和xpath方法
    -css中写css选择器
    -xpath中写xpath选择
    2 重点1:
    -xpath取文本内容
    './/a[contains(@class,"link-title")]/text()'
    -xpath取属性
    './/a[contains(@class,"link-title")]/@href'
    -css取文本
    'a.link-title::text'
    -css取属性
    'img.image-scale::attr(src)'
    3 重点2:
    .extract_first() 取一个
    .extract() 取所有
  • 相关阅读:
    linux 之 系统监控
    Spring Cloud Eureka 常用配置及说明
    mysql的事务隔离级别
    什么场景中会用到java多线程(转)
    springboot配置druid连接池
    MyBatis标签详解(转)
    关于@JsonSerialize注解的使用方法
    layer绑定回车事件(转)
    php7+apache2.4配置
    Eclipse创建Maven项目不支持el表达式的解决方式
  • 原文地址:https://www.cnblogs.com/DEJAVU888/p/14894069.html
Copyright © 2020-2023  润新知