程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python crawler selenium

編輯:Python

Catalog

  • brief introduction
    • website
    • Personal understanding
  • Environment building
  • selenium webdriver Use
  • Selenium application
    • Realization 12306 Drag login authentication instance
    • Use the super eagle to log in to the super Eagle website to verify the image

brief introduction

website

selenium Is the most widely used open source Web UI( The user interface ) One of the automated test suites .

Personal understanding

selenium: Automated test tool

Let my program connect to the browser , Let the browser complete all kinds of complex operations , We only accept the final result

You can open a browser . Then operate the browser like a person , Programmers can selenium Directly extract all kinds of information on the web page

Environment building

1、

pip install selenium -i Tsinghua source

2、 Download browser driver ; https://npm.taobao.org/mirrors/chromedriver
3、 Unzip the browser driver chromedriver Put it in python The folder where the interpreter is located
4、 Give Way selenium Launch Google browser

selenium webdriver Use

---- First create the browser object ( With Chrome For example )
driver = Chrome()

  1. Access to web pages , There are two ways to get web pages :

Use Get Method -driver.get("www.yiibai.com");

Use Navigate Method -driver.navigate().to("https://yiibai.com/selenium/");

  1. Find the form and send user input
web.find_element_by_xpath('//[@id="JuserName"]').send_keys('***')
  1. Clear user input
    clear() Method to clear user input from a text box .
driver.find_element_by_xpath().clear();
  1. adopt Web Element to get data
    Sometimes you need to get through web Element to perform certain assertions and debugging . Use getText() Method to get through any web Data written by element .
driver.find_element_by_xpath(**).getText();
  1. perform Click event
    click() Method is used for any Web Element to perform a click action .
driver.find_element_by_xpath(**).click();
  1. Navigate backward in browser history
driver.navigate().back();
  1. Navigate forward in browser history
driver.navigate().forward();
  1. Refresh / Reload the web page
driver.navigate().refresh();
  1. Close the browser
driver.close();
  1. Close the browser and all other windows associated with the driver
driver.quit();
  1. stay Windows Move between
driver.switch_to_window(web.window_handles[])

Return to previous window

web.switch_to_default_content()
  1. stay frame Move between
web.switch_to_frame(**)
  1. Drag and drop
    Use Action Class to perform drag and drop operations .
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));

14. Implementation of headless browser

opt = Options()
```java
# opt.add_argument("--headless")
# opt.add_argument("--disable-gpu")
opt.add_argument('--disable-blink-features=AutomationControlled')
#1. Create a browser object
web = Chrome(options=opt)

Selenium application

Realization 12306 Drag login authentication instance


from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
opt = Options()
# opt.add_argument("--headless")
# opt.add_argument("--disable-gpu")
opt.add_argument('--disable-blink-features=AutomationControlled')
#1. Create a browser object
web = Chrome(options=opt)
# 2. Open a web address
web.get("https://kyfw.12306.cn/otn/resources/login.html")
print(web.title)
web.find_element_by_xpath('//*[@id="toolbar_Div"]/div[2]/div[2]/ul/li[2]/a').click()
web.find_element_by_xpath('//*[@id="J-userName"]').send_keys(' user name ')
web.find_element_by_xpath('//*[@id="J-password"]').send_keys(' password ')
web.find_element_by_xpath('//*[@id="J-login"]').click()
time.sleep(3)
index = web.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span')
ActionChains(web).drag_and_drop_by_offset(index, 300, 0).perform()

Use the super eagle to log in to the super Eagle website to verify the image

from chaojiying import Chaojiying_Client
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
web = Chrome()
web.get("https://www.chaojiying.com/user/login/")
image = web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/div/img").screenshot_as_png
chaojiying = Chaojiying_Client(' user name ', ' password ', 'id')
print(chaojiying.PostPic(image, 1902))
dic = chaojiying.PostPic(image, 1902)
vs = dic['pic_str']
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input").send_keys(' user name ')
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input").send_keys(' password ')
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input").send_keys(vs,Keys.ENTER)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved