1、selenium剩余用法
from selenium import webdriver driver = webdriver.Chrome(r‘E:\python\chromedriver_win32\chromedriver.exe‘) import time try: webdriver.get("https://dig.chouti.com/") driver.implicity_wait(10) login_btn=driver.find_element_by_id(‘login_in‘) login_btn.click() time.sleep(2) phone=driver.find_element_by_class_name(‘‘) phone.send_keys(‘15622792660‘) pwd=driver.find_element_by_class_name(‘pwd-password-input‘) pwd.send_keys(‘kermit46709394‘) login_submit=driver.find_element_by_class_name(‘btn-large‘) login_submit.click() time.sleep(20) #捕获异常 except Exception as e: print(e) finally: driver.close()
Xpath语言
‘‘‘ Xpath语法: ‘‘‘ from selenium import webdriver driver = webdriver.Chrome(r‘E:\python\chromedriver_win32\chromedriver.exe‘) try: driver.get(‘dizhi‘) driver.implicitly_wait(5) #time.sleep(5) #根据xpath查找 #/从根开始找第一个 html=driver.find_element_by_xpath(‘/html‘) head=driver.find_element_by_xpath(‘/head‘)#报错 print(head.tag_name) #//从根开始找任意一个节点 div=driver.find_element_by_xpath(‘//div‘) print(div.tag_name) print(div.tag_name) #@ #查找ID为image的div节点 driver.find_element_by_xpath(‘//div[@id="image"]‘) print(div.tag_name) print(div.text) #找到第一个a节点 a=driver.find_element_by_xpath(‘//a‘) print(a.tag_name) #找到所有a节点 a_s = driver.find_elements_by_xpath(‘//a‘) print(a_s) #找到第一个a节点的href属性 #get_attribute:获取节点中的某个属性 a=driver.find_element_by_xpath(‘//a‘).get_attribute(‘href‘) #获取a中href属性 print(a) finally: driver.close()
selenium万能登录破解
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome(r‘E:\python\chromedriver_win32\chromedriver.exe‘) import time # try: # # # # #隐式等待:写在get请求前 # driver.implicitly_wait(10) # # #1、往jd发送请求 # driver.get(‘http://www.jd.com/‘) # # # #显式等待:写在get请求后 # #wait.until() # # #找到输入框输入围城 # input_tag=driver.find_element_by_id(‘key‘) # input_tag.send_keys(‘围城‘) # # #键盘回车 # input_tag.send_keys(Keys.ENTER) # time.sleep(2) # # # #找到输入框输入墨菲定律 # input_tag=driver.find_element_by_id(‘key‘) # input_tag.clear() # input_tag.send_keys(‘墨菲定律‘) # # #找到搜索按钮点击搜索 # button=driver.find_element_by_class_name(‘button‘) # button.click() # time.sleep(10) # finally: # driver.close() #获取cookie信息(了解) # try: # driver.implicitly_wait(10) # driver.get(‘http://www.zhihu.com/explore‘) # print(driver.get_cookies()) # # # # driver.add_cookie({‘Name‘:‘tank‘,‘pwd‘:‘123‘}) # # print(driver.get_cookie()) # time.sleep(10) # # finally: # driver.close() #添加选项卡 # driver.get(‘https://www.baidu.com‘) # driver.execute_script(‘window.open()‘)#执行jsp代码,window.open():打开新建浏览器串口,alter():弹窗操作 # # print(driver.window_handles) #获取所有的选项卡,返回列表 # driver.switch_to_window(driver.window_handles[1])#返回某一个选项卡,第二个窗口 # driver.get(‘https://www.taobao.com‘)#第二个窗口往淘宝发送请求 # time.sleep(10) # #切换到第一个窗口,向新浪发送请求 # driver.switch_to_window(driver.window_handles[0]) # driver.get(‘https://www.sina.com.cn‘) # driver.close() #异常 ‘‘‘ actionchangs动作链 ‘‘‘ from selenium import webdriver from selenium.webdriver import ActionChains import time driver = webdriver.Chrome(r‘E:\python\chromedriver_win32\chromedriver.exe‘) driver.implicitly_wait(10) driver.get(‘http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable‘) try: driver.switch_to.frame(‘iframeResult‘) #源位置 draggable=driver.find_element_by_id(‘draggable‘) #目标位置 droppable=driver.find_element_by_id(‘droppable‘) #调用ActionChains,必须把驱动对象传进去 #得到一个动作链对象,复制给一个变量 actions = ActionChains(driver) # 方式一: 机器人 # 瞬间把源图片位置秒移到目标图片位置 # actions.drag_and_drop(draggable, droppable) # 编写一个行为 # actions.perform() # 执行编写好的行为 # 方式二: 模拟人的行为 source = draggable.location[‘x‘] target = droppable.location[‘x‘] print(source, target) distance = target - source print(distance) # perform:每个动作都要调用perform执行 # 点击并摁住源图片 ActionChains(driver).click_and_hold(draggable).perform() s = 0 while s < distance: # 执行位移操作 ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform() s += 2 # 释放动作链 ActionChains(driver).release().perform() time.sleep(10) finally: driver.close() # ‘‘‘ # 前进、后退 # ‘‘‘ # from selenium import webdriver # import time # # driver = webdriver.Chrome() # # # try: # driver.implicitly_wait(10) # driver.get(‘https://www.jd.com/‘) # driver.get(‘https://www.baidu.com/‘) # driver.get(‘https://www.cnblogs.com/‘) # # time.sleep(2) # # # 回退操作 # driver.back() # time.sleep(1) # # 前进操作 # driver.forward() # time.sleep(1) # driver.back() # time.sleep(10) # # finally: # driver.close()
# #破解登录 # # from selenium import webdriver # from selenium.webdriver import ChromeOptions # import time # # ‘‘‘ # 步骤: # 1、打开文件的查看,显示隐藏文件 # 2、找到Google的地址中删除default文件 # 3、重新打开浏览器,并登录百度账号 # ——此时会重新创建一个新的default文件 # 4、添加cookies # 5、关闭谷歌浏览器后执行程序 # ‘‘‘ # # 获取options对象,参数对象 # options = ChromeOptions() # # # 获取cookies保存路径 # # ‘C:\Users\administortra\AppData\Local\Google\Chrome\User Data‘ # profile_directory = r‘--user-data-dir=C:\Users\李梦凡\AppData\Local\Google\Chrome\User Data‘ # # # 添加用户信息目录 # options.add_argument(profile_directory) # # # 把参数加载到当前驱动中 chrome_options默认参数,用来接收options对象 # driver = webdriver.Chrome(chrome_options=options) # # try: # driver.implicitly_wait(10) # driver.get(‘https://www.baidu.com/‘) # ‘‘‘ # BDUSS:***** # ‘‘‘ # # 添加用户cookies信息 # # name、value必须小写 # driver.add_cookie({"name": "BDUSS", "value": "lZVWF1Nk1ufkdhMVpYZWhyamZ1T05CV2hpVEhmTjg5RHV3cDVSREREeWw2QzlkSVFBQUFBJCQAAAAAAAAAAAEAAADzxTycaGprc2tra2pzagAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKVbCF2lWwhdcD; BDRCVFR[7MNccJyLkSD]=aeXf-1x8UdYcs"}) # # # 刷新操作 # driver.refresh() # # time.sleep(10) # # finally: # driver.close() from selenium import webdriver from selenium.webdriver import ChromeOptions import time r‘‘‘ 步骤: 1、打开文件的查看,显示隐藏文件 2、找到C:\Users\administortra\AppData\Local\Google\Chrome\User Data 删除Default文件 3、重新打开浏览器,并登陆百度账号 - 此时会创建一个新的Default缓存文件 ‘‘‘ # 获取options对象,参数对象 options = ChromeOptions() # 获取cookies保存路径 # ‘C:\Users\administortra\AppData\Local\Google\Chrome\User Data‘ profile_directory = r‘--user-data-dir=C:\Users\李梦凡\AppData\Local\Google\Chrome\User Data‘ # 添加用户信息目录 options.add_argument(profile_directory) # 把参数加载到当前驱动中 chrome_options默认参数,用来接收options对象 driver = webdriver.Chrome(chrome_options=options) try: driver.implicitly_wait(10) driver.get(‘https://www.baidu.com/‘) ‘‘‘ BDUSS:***** ‘‘‘ # 添加用户cookies信息 # name、value必须小写 driver.add_cookie({"name": "BDUSS", "value": "lZVWF1Nk1ufkdhMVpYZWhyamZ1T05CV2hpVEhmTjg5RHV3cDVSREREeWw2QzlkSVFBQUFBJCQAAAAAAAAAAAEAAADzxTycaGprc2tra2pzagAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKVbCF2lWwhdcD"}) # 刷新操作 driver.refresh() time.sleep(10) finally: driver.close()
破解极域滑动验证码
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome(r‘E:\python\chromedriver_win32\chromedriver.exe‘) import time ‘‘‘ 请求URL 1、商品详情页 2、商品名称 3、商品价格 4、评价人数 5、商品和是哪国家 ‘‘‘ def get_good(driver): try: # driver.impilitly_wait(10) # #1、往京东主页发送请求 # driver.get(‘https://www.jd.com/‘) # # # #2、输入商品名称,并回车搜索 # input_tag=driver.find_element_by_id(‘key‘) # input_tag.send_keys(‘macbook‘) # input_tag.send_keys(Keys.ENTER) #通过JS控制滚轮滑动获取所有商品信息 js_code=‘‘‘ window.scrollTo(0,500) ‘‘‘ driver.execute_script(js_code)#执行JS代码 time.sleep(2) #3、查找所有商品div good_list=driver.find_elements_by_class_name(‘gl-item‘) #n=1 for good in good_list: #根据属性选择器查找 #商品链接 good_url=good.find_element_by_css_selector( ‘.p-img a‘ ).get_attribute(‘href‘) #商品名称 good_name=good.find_element_by_css_selector( ‘.p-name em‘ ).text.replace("\n",‘:‘) #商品价格 good_price = good.find_element_by_class_name( ‘p-price‘).text.replace(‘\n‘,‘:‘) #评价人数 good_commit=good.find_element_by_class_name( ‘p-commit‘).text.replace(‘\n‘,‘ ‘) #商品商家 good_from=good.find_element_by_class_name( ‘J_im_icon‘).text.replace(‘\n‘,‘ ‘) good_content=f‘‘‘ 商品链接:{good_url} 商品,名称:{good_name} 商品价格:{good_price} 评价人数:{good_commit} 商品商家:{good_from} \n ‘‘‘ print(good_content) with open(‘jd.txt‘,‘a‘,encoding=‘utf-8‘) as f: f.write(good_content) next_tag=driver.find_element_by_class_name(‘pn-next‘) next_tag.click() time.sleep(2) #递归函数 get_good(driver) time.sleep(10) finally: driver.close() if __name__==‘__main__‘: good_name=input(‘请输入爬取商品的信息:‘).strip() # driver=webdriver.Chrome() driver.implicitly_wait(10) #往京东主页发送请求 driver.get(‘https://www.jd.com/‘) #2、输入商品名称,并回车搜索 input_tag = driver.find_element_by_id(‘key‘) input_tag.send_keys(good_name) input_tag.send_keys(Keys.ENTER) time.sleep(2) get_good(driver)
小结:今天又学到了一些技能,感觉自己棒棒的。tank比昨天更加的帅了!
原文地址:https://www.cnblogs.com/lmff/p/11048559.html
时间: 2024-10-10 00:42:19