关于python之selenium

为什么要学习selenium

下面这样直接去获取京东数据,是获取不到秒杀数据的,但是我们手动通过浏览器去查看,却是可以查看到的,所以我们需要去模拟人去操作浏览器访问,就可以获取到数据了

# _*_ coding : utf-8 _*_
# @Time : 2023/1/31 15:20
# @Author : 李阶熊
# @File : 为什么要学习selenium
# @Project : pythonProject
import urllib.request

url = 'https://www.jd.com/'

response = urllib.request.urlopen(url)

content = response.read().decode('utf-8')

print(content)

selenium基本使用

# _*_ coding : utf-8 _*_
# @Time : 2023/1/31 15:30
# @Author : 李阶熊
# @File : selenium基本使用
# @Project : pythonProject

# 1 导入selenium
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# 2 创建浏览器操作对象
path = 'chromedriver.exe'

s = Service(executable_path=path)
driver = webdriver.Chrome(service=s)

# 3 访问网站
url = 'https://www.jd.com/'

driver.get(url)

time.sleep(10)

content = driver.page_source

print(content)



selenium 元素定位

无界面

# _*_ coding : utf-8 _*_
# @Time : 2023/1/31 15:48
# @Author : 李阶熊
# @File : selenium元素定位
# @Project : pythonProject
# 1 导入selenium
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service


# 2 创建浏览器操作对象
path = 'chromedriver.exe'
s = Service(executable_path=path)
browser = webdriver.Chrome(service=s)

# 操作网站
url = 'https://www.baidu.com'

browser.get(url)

# 元素定位  根据id来找到对象
# button = browser.find_element(By.ID, 'su')
# button = browser.find_element('id', 'su')
# print(button)


# 根据标签属性的属性值来获取对象
# button = browser.find_element('name', 'wd')
# print(button)

# 根据xpath语句来获取对象的
# button = browser.find_element('xpath', '//input[@id="su"]')
# print(button)

# 根据标签的名字来获取对象
# button = browser.find_elements('tag name', 'input')
# print(button)

# 使用的bs4语法来获取对象
# button = browser.find_elements('css selector', '#su')
# print(button)


button = browser.find_element('link text', '视频')
print(button)
time.sleep(4)



selenium 交互

# _*_ coding : utf-8 _*_
# @Time : 2023/1/31 17:37
# @Author : 李阶熊
# @File : selenium交互
# @Project : pythonProject
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

path = 'chromedriver.exe'
s = Service(executable_path=path)
browser = webdriver.Chrome(service=s)

# url
url = 'https://www.baidu.com'
browser.get(url)

time.sleep(2)
# 获取文本框的对象
input_ = browser.find_element('id', 'kw')

# 在文本框中输入周杰伦
input_.send_keys('周杰伦')

time.sleep(2)

# 获取百度一下的按钮
button = browser.find_element('id', 'su')

# 点击按钮
button.click()

time.sleep(2)

# 划到底部

js_bottom = 'document.documentElement.scrollTop=100000'
browser.execute_script(js_bottom)

time.sleep(2)

# 获取下一页的按钮
next_ = browser.find_element('xpath', '//a[@class="n"]')

# 点击下一页
next_.click()

time.sleep(2)

# 回到上一页
browser.back()

time.sleep(2)

# 回去
browser.forward()

time.sleep(5)

browser.quit()

元素信息以及交互

# _*_ coding : utf-8 _*_
# @Time : 2023/1/31 17:21
# @Author : 李阶熊
# @File : selenium_元素信息以及交互
# @Project : pythonProject
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

path = 'chromedriver.exe'
s = Service(executable_path=path)
browser = webdriver.Chrome(service=s)

url = 'https://www.baidu.com'
browser.get(url)

input_ = browser.find_element('id', 'su')
# 获取标签的属性
print(input_.get_attribute('class'))
print(input_.tag_name)

a = browser.find_element('link text', '新闻')
# 获取元素文本
print(a.text)

selenium 无界面headless

# _*_ coding : utf-8 _*_
# @Time : 2023/2/2 10:37
# @Author : 李阶熊
# @File : selenium_headless
# @Project : pythonProject
# 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')
#
# # path是你自己的chrome浏览器的文件路径
# path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
# chrome_options.binary_location = path
# browser = webdriver.Chrome(chrome_options=chrome_options)
#
# url = 'https://www.baidu.com'
#
# browser.get(url)
#
# browser.save_screenshot('baidu.png')


from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def share_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')

    # path是你自己的chrome浏览器的文件路径
    path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'

    chrome_options.binary_location = path
    browser = webdriver.Chrome(chrome_options=chrome_options)
    return browser


browser = share_browser()

url = 'https://www.baidu.com'

browser.get(url)

browser.save_screenshot('baidu.png')

猜你喜欢

转载自blog.csdn.net/weixin_49177159/article/details/128837747