zoukankan      html  css  js  c++  java
  • 爬虫——selenium模块

    selenium使用

    为了解决requests无法直接执行JavaScript代码的问题

    安装

    pip3 install selenium
    

    浏览器驱动

    浏览器驱动:http://npm.taobao.org/mirrors/chromedriver/
    驱动要跟浏览器版本对应  84.0.4147.105:驱动用84.0.4147.30/
    
    from selenium import webdriver
    import time
    
    # 指定驱动
    bro=webdriver.Chrome(executable_path='./chromedriver.exe') # 得到一个谷歌浏览器对象,
    time.sleep(2)
    bro.get('https://www.baidu.com/')  # 在地址栏里输入了百度
    time.sleep(2)
    print(bro.page_source)
    time.sleep(2)
    bro.close()
    
    
    
    from selenium import webdriver
    import time
    bro=webdriver.Chrome(executable_path='./chromedriver.exe')
    bro.implicitly_wait(5)  
    # 隐式等待:找一个控件,如果控件没有加载出来,等待5s中  等待所有,只需要写这一句,找所有控件都按这个操作来
    bro.get('https://www.baidu.com/')
    
    d_button=bro.find_element_by_link_text('登录')
    d_button.click()
    login_u=bro.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn')
    login_u.click()
    
    username=bro.find_element_by_id('TANGRAM__PSP_11__userName')
    username.send_keys('yxp654799481')
    password=bro.find_element_by_id('TANGRAM__PSP_11__password')
    password.send_keys('yxp997997')
    time.sleep(3)
    submit=bro.find_element_by_id('TANGRAM__PSP_11__submit')
    
    submit.click()
    time.sleep(10)
    
    print(bro.get_cookies())
    
    bro.close()
    

    异常处理

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException,NoSuchElementException,NoSuchFrameException
    browser=webdriver.Chrome()
    try:
        browser.get('')
    except Exception as e:
        print(e)
    finally:
       # 无论是否出异常,最终都要关掉
        browser.close()
    

    浏览器全屏

    bro.set_window_size(1920, 1080)
    bro.maximize_window()
    

    选择器(find系列)

    find_element_by_id   # 通过id查找控件
    find_element_by_link_text  # 通过a标签内容找
    find_element_by_partial_link_text  # 通过a标签内容找,模糊匹配
    find_element_by_tag_name   # 标签名
    find_element_by_class_name  # 类名
    find_element_by_name      # name属性
    find_element_by_css_selector  # 通过css选择器
    find_element_by_xpath       # 通过xpaht选择器
    
    find_elements_by_xxx的形式是查找到多个元素,结果为列表
    
    # 获取元素属性
    tag.get_attribute('href')  # 找当前控件 的href属性对的值
    tag.text   # 获取文本内容
    
    print(tag.id)   # 当前控件id号
    print(tag.location)  # 当前控件在页面位置
    print(tag.tag_name)  # 标签名
    print(tag.size)      #标签的大小
    

    无界面浏览器(phantomjs)

    #谷歌浏览器支持不打开页面
    from selenium.webdriver.chrome.options import Options
    from selenium import webdriver
    chrome_options = Options()
    chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
    chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
    chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
    chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
    
    chrome_options.add_argument('--headless') #浏览器不提供可视化页面. 
    linux下如果系统不支持可视化不加这条会启动失败
    
    bro=webdriver.Chrome(chrome_options=chrome_options,executable_path='./chromedriver.exe')
    bro.get('https://www.baidu.com/')
    print(bro.page_source)
    bro.close()
    

    元素交互

    tag.send_keys()  # 往里面写内容
    tag.click()      # 点击控件
    tag.clear()      # 清空控件内容
    

    执行js

    bro.execute_script('window.open()')
    
    把屏幕拉倒最后(js控制)
    bro.execute_script('window.scollTo(0,document.body.offsetHeight)')
    

    操作浏览器前进后退

    browser.back()
    browser.forward()
    

    获取cookies

    bro.get_cookies()
    

    选项卡管理

    print(browser.window_handles) #获取所有的选项卡
    browser.switch_to_window(browser.window_handles[1])
    browser.switch_to_window(browser.window_handles[0])
    

    动作链

    from selenium.webdriver import ActionChains
    
    ActionChains(bro).move_to_element_with_offset(img_t, x, y).click().perform()
    

    模拟键盘按键输入

    from selenium.webdriver.common.keys import Keys
    
    input_k.send_keys(Keys.ENTER)
    
  • 相关阅读:
    洛谷P1613 跑路
    洛谷P2149 Elaxia的路线
    洛谷P3119 草鉴定
    洛谷P1972 HH的项链
    洛谷P2458 保安站岗
    uva10061
    uva579
    uva 127 "Accordian" Patience
    uva10177 (2/3/4)-D Sqr/Rects/Cubes/Boxes?
    uva156
  • 原文地址:https://www.cnblogs.com/pythonwl/p/13444461.html
Copyright © 2011-2022 走看看