First imagine that you only need a command, and then the browser will operate on its own according to the command you enter, without having to do it yourself.
Open the browser automatically
Open the browser automatically. If this is simply opening a web page, python has a built-in module, as follows:
import webbrowser webbrowser.open("http://ww.baidu.com") #The URL you need to open
It automatically opens the default browser and displays the web page.
However, if the purpose of the operation cannot be achieved, what should we do? Let's import another module and the corresponding browser driver, and then store the driver in the python root directory.
Need to install, pip install selenium
Import
from selenium import webdriver
driver=webdriver.Chrome()
#url to open the URL to be opened
driver.get(url="http://www.baidu.com")
I opened the website, what should I do next?
Of course it was done.
First, adjust the browser interface to the maximum.
#Maximize the current window driver.maximize_window()
Then we come to the operation, there are two main operations commonly used,
Click click()
Enter send_key()
The operation is there, but you always have to tell me where to order!
Positioning.
Targeting:
#Locating element by class name driver.find_element_by_class_name("")
#Locating driver
by css.find_element_by_css_selector("")
#Locating driver
by id.find_element_by_id("")
#Locating driver
by link name.find_element_by_link_text("") #By
name Locate
driver.find_element_by_name("") #Locate
via link address
driver.find_element_by_partial_link_text("")
#Locate driver
via xpath.find_element_by_xpath("")
#Locate driver
via tag.find_element_by_tag_name("")
Coherent operation:
#Locate by link name, click on Baidu driver.find_element_by_link_text("Baidu click").click()
Other operations, screenshots, and save files.
#Screenshotdriver.get_screenshot_as_file
("Screenshot.png")
Close the browser
#Close the browser driver.close()
selenium crawler
Of course, the automatic operation of the browser is done. Here I will mention the crawler features of selenium. It can also be used to make your own crawler. Is it very powerful? Of course, the crawler is ignored. Mention here, if you are interested, you can find out!
O ^ ~ ^ O
Download pictures and never do it yourself
A python crawler tutorial that you have to know
Welcome to pay attention to the official account, and reply to " Operate the browser " in the background to obtain the browser driver of the corresponding version. Of course, you can use other ones as long as you can.