爬虫就三大种实现方式。第一种是获取网页源代码来获取数据,这种也只能在特定网站生效;第二种是直接get或者post提交数据,这种难度有时候会较大,因为接口往往有一些限制;第三种就是直接模拟浏览器点击操作,第三种就比较无脑,可以实现很多第一第二种实现不了,比如boss海投工具等。本文介绍的就是selenium的基本用法。
python3中使用selenium进行web自动化测试是一种非常常见的方法。
#python3中selenium使用方法(连载)  https://www.toutiao.com/a6425040355548397825/
# selenium 调用iedriverserver打开ie浏览器 - 天高地阔的专栏 - csdn博客  
#2018/2/*
#http://blog.csdn.net/jichuang123/article/details/53008581
from selenium import webdriver
from selenium.webdriver.common.keys import keys
import time
brower = webdriver.ie()
url = 'https://hao.360.cn/?wd_xp1'
brower.get(url)
intput = brower.find_element_by_id('search-kw')
intput.send_keys('python')
intput.send_keys(keys.enter)
time.sleep(3)
brower.quit()知识点
1.新版本的写法(需要写service,否则好像会报错)
from selenium import webdriver  
from selenium.webdriver.chrome.service import service  
from selenium import webdriver  
from webdriver_manager.chrome import chromedrivermanager
s = service(path)      # path为chromedriver安装路径
bro = webdriver.chrome(service=s)  
bro.get("https://www.shixiseng.com/")2.chromedriver安装对应浏览器版本的方法(推荐这种方法安装驱动!可以避免版本不一致的问题)
 (会安装在类似“c:\users\czh\.wdm\drivers\chromedriver\win32\107.0.5304\chromedriver.exe”的文件夹下)
from webdriver_manager.chrome import chromedrivermanager
# 下载驱动  
def downdriver():  
    driver = webdriver.chrome(chromedrivermanager().install())3.根据classname找元素的方法
 (1)class name有空格的写法
bro.find_element('css selector', "[class='tiktok-q9aj5z-pcommenttext e1g2efjf6']")(2)class name(点开头)
bro.find_element('css selector', ".qaq")4.根据class的id找元素的方法
 class id(#开头)
bro.find_element('css selector', "#qaq")5.点击按钮写法
button = bro.find_element('css selector', ".qaq")
button.click()6.如果找同个classname的多个元素,在find_element补个s即可(然后用for循环遍历)
bro.find_elements('css selector', ".qaq")7.如果等待页面加载出某个元素再进行下一步操作,用try,封装函数如下
# 检查存在再进行数据的  
def check_find_elements(bro,by,value):  
    # 等待元素加载完毕  
    while (true):  
        try:  
            # 注意.是class name的意思  
            bro.find_element(by, value)  
            #print(value + "找到")  
            break  
        except exception:  
            continue  
    return bro.find_elements(by, value)  
  
# 检查存在再进行数据的  
def check_find_element(bro,by,value):  
    # 等待元素加载完毕  
    while (true):  
        try:  
            # 注意.是class name的意思  
            bro.find_element(by, value)  
            #print(value + "找到")  
            break  
        except exception:  
            continue  
    return bro.find_element(by, value)现在就可以写成check_find_element(bro, "css selector","[class='nickname']")就会在元素加载完成后再进行下步操作
8.打开了新窗口后,想进入新窗口完成操作,结束后回到上个窗口
# 进入新窗口
cls = bro.window_handles  
bro.switch_to.window(cls[1])
# 这里应该进行新窗口的操作
# 关闭新窗口,回到旧窗口
bro.close()  
bro.switch_to.window(cls[0])9.浏览器最小化,窗口缩小
bro.set_window_size(300, 300)  
bro.minimize_window()       # 最小化窗口,不影响
 
             我要评论
我要评论 
                                             
                                             
                                             
                                             
                                            
发表评论