爬虫+自动化
from selenium import webdriver
import time
wb=webdriver.Chrome()
wb.get(‘http://www.baidu.com’)
print(wb.title)
time.sleep(5)
wb.quit()
#无界面模式
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options() #实例化
chrome_options.add_argument(’–headless’)
chrome_options.add_argument(’–disable-gpu’)
wb = webdriver.Chrome(options=chrome_options)
wb.get(‘https://www.icourse163.org/learn/ZJU-93001?tid=1002654021#/learn/content?type=detail&id=1003620976&sm=1’)
print(wb.title)
wb.quit()
#最大化最小化窗口
from selenium import webdriver
import time
wb =webdriver.Chrome()
wb.get(‘https://www.bilibili.com/’)
wb.maximize_window()
time.sleep(2)
wb.set_window_size(400,800)
time.sleep(2)
wb.quit()
网页的前进后退刷新
from selenium import webdriver
import time
wb = webdriver.Chrome()
wb.get(‘https://www.taobao.com/’)
time.sleep(3)
wb.get(‘https://www.jd.com/’)
time.sleep(3)
wb.back()
time.sleep(3)
wb.forward()
wb.refresh()
定位网页元素
元素定位的八种方式
id
xpath
link text
partial_link_text
name
tag_name
class_name
css_selector
from selenium import webdriver
wb = webdriver.Chrome()
wb.get(‘https://www.baidu.com/’)
input_ = wb.find_element_by_id(‘kw’)
input_.send_keys(‘最好的编程语言’)
output_ = wb.find_element_by_id(‘su’)
output_.click()
wb.quit()