合肥制作网站公司,系统开发过程中原型有哪些作用,asp个人网站论文,免费国外服务器ipselenium基本使用
这个模块#xff1a;既能发请求#xff0c;又能解析#xff0c;还能执行js
selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行 JavaScript代码的问题
selenium 会做web方向的自动化测试appnium 会做 app方向的自动化…selenium基本使用
这个模块既能发请求又能解析还能执行js
selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行 JavaScript代码的问题
selenium 会做web方向的自动化测试appnium 会做 app方向的自动化测试
selenium 可以操作浏览器模拟人的 行为
使用浏览器 下载浏览器驱动chrome https://registry.npmmirror.com/binary.html?pathchromedriver/https://googlechromelabs.github.io/chrome-for-testing/https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/119.0.6045.105/win64/chromedriver-win64.zip火狐驱动https://github.com/mozilla/geckodriver/releases/跟浏览器型号和版本一一对应的 ie火狐谷歌谷歌为例 谷歌浏览器有很多版本跟版本一一对应 将驱动放到python解释器目录下或者配置环境变量 下载模块pip install selenium 写python代码操作浏览器
import time
from selenium import webdriver# 跟人操作浏览器一样打开了谷歌浏览器拿到浏览器对象
browebdriver.Firefox()# 在地址栏中输入地址
bro.get(https://www.baidu.com)
time.sleep(5)
bro.close()指令
bro为实例化所得对象
在地址栏中输入地址bro.get(网址地址)关闭浏览器bro.close()设置等待bro.implicitly_wait(10)从页面中找标签如果找不到就等待页面最大化bro.maximize_window()当前页面html内容bro.page_source)选择器 from selenium.webdriver.common.by import By 找一个bro.find_element(byBy.选择器,value)找所有bro.find_elements(byBy.选择器,value) 点击找到的标签.click()文本框写入找到的标签.send_keys()
模拟登录
from selenium import webdriver
from selenium.webdriver.common.by import Bybro webdriver.Firefox()
bro.get(https://www.baidu.com)
bro.implicitly_wait(10)
bro.maximize_window()# 找到登录按钮
a_login bro.find_element(byBy.LINK_TEXT, value登录)
a_login.click()# 往输入框中写文字
username bro.find_element(byBy.ID, valueTANGRAM__PSP_11__userName)
username.send_keys(13437238745)
password bro.find_element(byBy.ID, valueTANGRAM__PSP_11__password)
password.send_keys(caimina1)agree bro.find_element(By.ID, TANGRAM__PSP_11__isAgree)
agree.click()submit bro.find_element(By.ID, TANGRAM__PSP_11__submit)
submit.click()bro.close()selenium其它用法
无头浏览器(chrome浏览器)
如果我们做爬虫我们只是为了获取数据不需要非有浏览器在显示 ⇢ \dashrightarrow ⇢ 隐藏浏览器图形化界面
chrome
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
chrome_options Options()
chrome_options.add_argument(blink-settingsimagesEnabledfalse) #不加载图片, 提升速度
chrome_options.add_argument(--headless) #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
bro webdriver.Chrome(optionschrome_options)bro.get(https://www.cnblogs.com/liuqingzheng/p/16005896.html)print(bro.page_source)
time.sleep(3)
bro.close()FireFox
from selenium import webdriveroptions webdriver.FirefoxOptions()
options.add_argument(--headless) # 设置火狐为headless无界面模式
options.add_argument(--disable-gpu)
driver webdriver.Firefox(optionsoptions)
driver.get(https://www.qq.com)
print(driver.page_source)
driver.close() 搜索标签
搜索标签
根据id号查找标签根据name属性查找标签根据标签查找标签按类名找a标签文字a标签文字模糊匹配按css选择器找按xpath找
获取标签的属性文本大小位置
属性bro.get_attribute(src)文本bro.text大小tag.size位置bro.locationid不是标签id无需关注tag.id标签名tag.tag_name
找到页面中所有div
divsbro.find_elements(By.TAG_NAME,div)按类名找
divbro.find_element(By.CLASS_NAME,postDesc).text按css选择器
divbro.find_element(By.CSS_SELECTOR,div.postDesc).text#id为topics下的div下的div中类为postDesc
divbro.find_element(By.CSS_SELECTOR,#topics div div.postDesc).text