已解决
Selenium 基本功能
来自网友在路上 162862提问 提问时间:2023-11-03 18:41:01阅读次数: 62
最佳答案 问答题库628位专家为你答疑解惑
#driver.quit()
'''
from selenium import webdriver
from selenium.webdriver.chrome.service import Service# 尝试传参
s = Service("chromedriver.exe")
driver = webdriver.Chrome(service=s)driver.get('https://www.baidu.com/')
input()
'''
#1/导入Selenium库
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options#2创建一个浏览器实例
driver_path='/Users/yanghao31/Desktop/software/chromedriver.exe'
options=Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)#3/使用浏览器实例访问网页并模拟人类操作
driver.get('http://www.baidu.com')wait = WebDriverWait(driver,10,0.2)#等待渲染
elem = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'.s_ipt'))) #查找输入框
elem.send_keys('Hello world') #模拟输入搜索项clickEle=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'.s_btn_wr > input')))clickEle.click()driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(3)
# 向上滚动到顶部
driver.execute_script('window.scrollTo(0,0)')
time.sleep(3)
# 4,获取网页中的动态加载内容
print(driver.page_source)# 5,关闭浏览器
driver.quit()
# 启动Chrome
import osfrom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import requests
from parsel import Selector
from fake_useragent import UserAgentdef getInformation(html,cssString):sel=Selector(text=html)itmes=sel.css(cssString).getall()return itmesdef getactle(html,saveDir):cssactle='.ChapterContent_bible-reader__Du4dP'actile=getInformation(html,cssactle)[0]cssTitle='.ChapterContent_reader__UZc2K h1::text'title=getInformation(actile,cssTitle)[0]text=''Cssrow='.ChapterContent_p___9Q1l, .ChapterContent_heading__RjEme'rows=getInformation(actile,Cssrow)for row in rows:cssText='.ChapterContent_content__dkdqo *::text'texts=getInformation(row,cssText)if len(texts)==0:cssText = '.ChapterContent_heading__RjEme *::text'texts = getInformation(row, cssText)textFinal=''.join(texts)text+=textFinal+'\n'savePath=os.path.join(saveDir,title+'.txt')with open(savePath,'w',encoding='utf-8') as file:file.write(title)file.write(text)return title,textfrom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
def GetHtml_run(url):options = webdriver.ChromeOptions()options.add_experimental_option("detach", True)options.add_argument(f'user-agent={UserAgent().random}')service = Service(executable_path='/Users/yanghao31/Desktop/software/chromedriver_mac_arm64/chromedriver')driver = webdriver.Chrome(service=service)print('准备完毕')# 设置页面加载超时时间为10秒钟driver.set_page_load_timeout(30)try:driver.get(url)# 等待页面完全加载完成driver.implicitly_wait(30)#WebDriverWait(driver, 30).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')html = driver.page_sourceexcept TimeoutException:print("页面加载超时")html = ''driver.quit()return html
from selenium.webdriver.chrome.options import Options
def GetHtml_TitleAndDes(url):options = Options()options.add_argument('--headless')options.add_argument(f'user-agent={UserAgent().random}')options.add_argument('--disable-gpu')options.add_argument('--no-sandbox')options.add_argument('--disable-dev-shm-usage')driver = webdriver.Chrome(options=options)print('准备完毕')# 设置页面加载超时时间为10秒钟driver.set_page_load_timeout(30)try:driver.get(url)# 等待页面完全加载完成driver.implicitly_wait(30)#WebDriverWait(driver, 30).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')# html = driver.page_source# 获取网站标题title = driver.title# 获取网站描述description = driver.find_element(by=By.CSS_SELECTOR,value='meta[name="description"]').get_attribute('content')except TimeoutException:print("页面加载超时")html = ''title, description='',''html=driver.page_sourcecssdes='meta[name="description"]::attr(content)'des1=getInformation(html,cssdes)csstitle='title *::text'title1=getInformation(html,csstitle)print({"Title":title1,"Des":des1})driver.quit()return title,descriptiondef main():textPath='/Users/yanghao31/Desktop/test/SeleniumTest/text'audioPath='/Users/yanghao31/Desktop/test/SeleniumTest/audio'if not os.path.exists(textPath):os.makedirs(textPath)if not os.path.exists(audioPath):os.makedirs(audioPath)options = webdriver.ChromeOptions()options.add_experimental_option("detach", True)options.add_argument(f'user-agent={UserAgent().random}')service = Service(executable_path='/Users/yanghao31/Desktop/software/chromedriver_mac_arm64/chromedriver')driver = webdriver.Chrome(service=service)print('准备完毕')driver.get('https://www.flaru.com/en/soundgasm.net/M4F-audio-nsfw') # 打开百度html = driver.page_sourceprint(html)a=input()
def GetInformation(html,cssString):sel= Selector(text=html)items=sel.css(cssString).getall()return itemsif "__main__" == __name__:url='https://www.pinterest.jp/search/pins/?q=natural%20japanese%20makeup&rs=typed'html=GetHtml_run(url)print(html)hrefs='a.Wk9.CCY.S9z.ho-.kVc.xQ4::attr(href)'href=GetInformation(html,hrefs)print(href)
查看全文
99%的人还看了
相似问题
- 【Vue3】解决Vue打包后上传服务器 资源路径加载错误
- ArcGIS如何处理并加载Excel中坐标数据?
- JVM:字节码文件,类的生命周期,类加载器
- 从复杂大模型加载到3D PDF发布: EVGET HOOPS Framework如何助力高性能3D桌面应用开发?
- PHP手动为第三方类添加composer自动加载
- Arcgis 日常天坑问题2——三维场景不能不能加载kml图层,着手解决这个问题
- 如何在el-tree懒加载并且包含下级的情况下进行数据回显-02
- Arcgis js Api日常天坑问题3——加载geojson图层,元素无属性
- 深度学习(五)softmax 回归之:分类算法介绍,如何加载 Fashion-MINIST 数据集
- 【CASS精品教程】cass3d 11.0加载超大影像、三维模型、点云数据
猜你感兴趣
版权申明
本文"Selenium 基本功能":http://eshow365.cn/6-31314-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!