得到app官网东莞网站排名优化报价
- 作者: 五速梦信息网
- 时间: 2026年03月21日 11:26
当前位置: 首页 > news >正文
得到app官网,东莞网站排名优化报价,什么叫网站流量,厦门手机网站1 selenium等待元素加载 2 selenium元素操作 3 selenium操作浏览器执行js 4 selenium切换选项卡 5 selenium前进后退异常处理 6 登录cnblogs 7 抽屉半自动点赞 8 xpath 9 动作链 10 自动登录12306 上节回顾
1 bs4 解析库—》xml(html)-遍历文档树-属性 文本 标签名-搜索文…1 selenium等待元素加载 2 selenium元素操作 3 selenium操作浏览器执行js 4 selenium切换选项卡 5 selenium前进后退异常处理 6 登录cnblogs 7 抽屉半自动点赞 8 xpath 9 动作链 10 自动登录12306
上节回顾
1 bs4 解析库—》xml(html)-遍历文档树-属性 文本 标签名-搜索文档树find find_all-5 种过滤器字符串正则布尔列表方法-find(name字符串,src正则,class_布尔,href列表,text字符串,方法)-遍历文档树和搜索文档树可以连用find() 结果是Tag类的对象BeautifulSoup继承了它结果可以继续 . 也可以继续 find-find的其他参数-limit-recursive-链式调用-小说图片# 2 css选择器-id选择器-标签选择器-类选择器-属性选择器- 标签- .类- #id- div a- divasoup.select(css选择器) 可能是多个放到列表中# 3 selenium :控制浏览器解决requests不能执行js的问题-浏览器驱动浏览器版本-pip install selenium-快速使用import timefrom selenium import webdriver# 跟人操作浏览器一样打开了谷歌浏览器拿到浏览器对象browebdriver.Chrome()# 在地址栏中输入地址bro.get(https://www.baidu.com)time.sleep(5)bro.close() # 关闭当前tab页bro.quit() # 关闭浏览器# 4 模拟登录百度-查找标签# By.ID # 根据id号查找标签# By.NAME # 根据name属性查找标签# By.TAG_NAME # # 根据标签查找标签# By.CLASS_NAME # 按类名找# By.LINK_TEXT # a标签文字# By.PARTIAL_LINK_TEXT # a标签文字模糊匹配#———selenium 自己的——–# By.CSS_SELECTOR # 按css选择器找# By.XPATH #按xpath找-操作标签-点击-写文字# 5 无头浏览器-bro.page_source# 6 找到标签-属性-文本-标签名-位置-大小1 selenium等待元素加载
代码操作非常快—》有的标签还没加载—》找就找不到—》就会报错
设置等待显示等待隐士等待
bro.implicitly_wait(10) # 找某个标签如果找不到最多等待10s2 selenium元素操作
点击操作click()# 写文字send_keys(内容)# 清空文字clear()3 执行js
在使用selenium操作浏览器的时候可以自己写js执行会用这东西做什么-创建新的选项卡-打印出一些变量属于当前爬取的页面中的变量-获取当前登录的cookie-滑动屏幕import timefrom selenium import webdriver
from selenium.webdriver.common.by import By bro webdriver.Chrome() bro.get(https://www.pearvideo.com/category_1) bro.implicitly_wait(10) bro.maximize_window()
1 基本使用
bro.execute_script(alert(美女))
2 打印出一些变量
resbro.execute_script(console.log(urlMap))
print(res)# 3 新建选项卡
bro.execute_script(open())# 4 滑动屏幕
bro.execute_script(scrollTo(0,document.documentElement.scrollHeight))# 5 获取当前访问地址
bro.execute_script(alert(location))
bro.execute_script(locationhttp://www.baidu.com)# 6 打印cookie
bro.execute_script(alert(document.cookie)) time.sleep(10) bro.close()4 切换选项卡 from selenium import webdriver import time bro webdriver.Chrome() bro.get(https://www.pearvideo.com/) bro.implicitly_wait(10) print(bro.window_handles)
开启选项卡
bro.execute_script(window.open())
获取出所有选项卡bro.switch_to.window(bro.window_handles[1]) # 切换到某个选项卡
bro.get(http://www.taobao.com)time.sleep(2) bro.switch_to.window(bro.window_handles[0]) # 切换到某个选项卡 bro.get(http://www.baidu.com)time.sleep(2) bro.execute_script(window.open()) bro.execute_script(window.open()) bro.close() # 关闭选项卡 bro.quit() # 关闭页面5 前进后退异常处理 from selenium import webdriver import time bro webdriver.Chrome() bro.get(https://www.pearvideo.com/) bro.implicitly_wait(10)# 获取出所有选项卡 time.sleep(2) bro.get(http://www.taobao.com)time.sleep(2)bro.get(http://www.baidu.com) time.sleep(2) bro.back() time.sleep(2) bro.back() time.sleep(2) bro.forward() bro.quit() # 关闭页面6 登录cnblogs
以后要爬取的数据要登录后才能看到-如果使用selenium速度慢—》不能开启多线程—》速度不会太快-如果使用requests发送请求登录不好登录自动登录不进去–》拿不到cookie-使用selenium登录—》拿到cookie—》换到别的机器使用这个cookie依然是登录状态import timefrom selenium import webdriver
from selenium.webdriver.chrome.options import Options import json from selenium.webdriver.common.by import By
去掉自动化软件控制的检测
options Options() options.add_argument(–disable-blink-featuresAutomationControlled) # 去掉自动化控制 bro webdriver.Chrome(optionsoptions)
bro webdriver.Chrome()
########### 纯自动登录#######得到了cookie bro.get(https://www.cnblogs.com/) bro.implicitly_wait(10) bro.maximize_window() login_btn bro.find_element(By.LINK_TEXT, 登录) login_btn.click()time.sleep(2)# 找到用户名和密码输入框 username bro.find_element(By.CSS_SELECTOR, #mat-input-0) password bro.find_element(By.ID, mat-input-1)submit_btn bro.find_element(By.CSS_SELECTOR,body app-root app-sign-in-layout div div app-sign-in app-content-container div div div form div button)
验证码
codebro.find_element(By.ID,rectMask) time.sleep(1)username.send_keys(qq.com) time.sleep(1) password.send_keys(#) time.sleep(1) submit_btn.click() # 一种情况直接登录成功 一种情况会弹出验证码 code.click() time.sleep(10)# 让程序先停在这—》手动操作浏览器—》把验证码搞好—》程序再继续往下走
到现在是登录成功的状态
取出cookie存起来
cookies bro.get_cookies() with open(cnblogs.json, w, encodingutf-8) as f:json.dump(cookies, f)time.sleep(2) bro.close() import timefrom selenium import webdriver from selenium.webdriver.chrome.options import Options import json from selenium.webdriver.common.by import By# 去掉自动化软件控制的检测 options Options() options.add_argument(–disable-blink-featuresAutomationControlled) # 去掉自动化控制 bro webdriver.Chrome(optionsoptions)bro.get(https://www.cnblogs.com/) bro.implicitly_wait(10) bro.maximize_window()time.sleep(5)
取出cookie–》写入到浏览器中—》刷新浏览器—》登录状态
with open(cnblogs.json, r) as f:cookies json.load(f)
写到浏览器中
for item in cookies:bro.add_cookie(item) # 如果是没登录的cookie往里写会报错# 刷新浏览器 bro.refresh()time.sleep(5) bro.close() 7 抽屉半自动点赞
使用selenium登录—》拿到cookie
点赞 使用requests 用cookie点赞
####自动登录—使用selenium#### import json# import time #
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import json
from selenium.webdriver.common.by import By
#
bro webdriver.Chrome()
bro.get(https://dig.chouti.com/)
bro.implicitly_wait(10)
bro.maximize_window()
#
btn_login bro.find_element(By.ID, login_btn)
time.sleep(1)
btn_login.click()
time.sleep(1)
#
phone bro.find_element(By.NAME, phone)
password bro.find_element(By.CSS_SELECTOR,
body div.login-dialog.dialog.animated2.scaleIn div div.login-footer div.form-item.login-item.clearfix.mt24 div input.input.pwd-input.pwd-input-active.pwd-password-input)
#
submit_login bro.find_element(By.CSS_SELECTOR,
body div.login-dialog.dialog.animated2.scaleIn div div.login-footer div:nth-child(4) button)
#
phone.send_keys(你的手机号)
password.send_keys(你的密码)
time.sleep(2)
submit_login.click()
#
input(等你)
#
cookies bro.get_cookies()
with open(chouti.json, w, encodingutf-8) as f:
json.dump(cookies, f)
#
time.sleep(2)
bro.close()#### 使用requests点赞# 访问首页解析出id号
import requests from bs4 import BeautifulSoup#### 携带cookie访问##### session requests.Session() cookie {} # 本地取出来写入 with open(chouti.json, r) as f:cookie_list json.load(f)
selenium的cookie和requests的cookie格式不一样要转换 {key:value,key:value}
for item in cookie_list:cookie[item[name]] item[value] header{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36} res session.get(https://dig.chouti.com/, cookiescookie,headersheader) soup BeautifulSoup(res.text, html.parser)print(res.text)divs soup.find_all(namediv, class_link-item) for div in divs:article_id div.attrs.get(data-id)data {linkId: article_id}res1 session.post(https://dig.chouti.com/link/vote, datadata,headersheader)print(res1.text) 8 xpath
在 xml中查找元素解析库-解析库自带的-css选择器-xpath—》通用的—》即为XML路径语言XML Path Language它是一种用来确定XML文档中某部分位置的语言# 记住的nodename 选取此节点的所有子节点。/ 从根节点选取。// 从匹配选择的当前节点选择文档中的节点而不考虑它们的位置。. 选取当前节点。.. 选取当前节点的父节点。 选取属性。
doc
htmlheadbase hrefhttp://example.com/ /titleExample website/title/headbodydiv idimagesa hrefimage1.html idlqzName: My image 1 br /img srcimage1_thumb.jpg //aa hrefimage2.htmlName: My image 2 br /img srcimage2_thumb.jpg //aa hrefimage3.htmlName: My image 3 br /img srcimage3_thumb.jpg //aa hrefimage4.htmlName: My image 4 br /img srcimage4_thumb.jpg //aa hrefimage5.html classli li-item nameitemsName: My image 5 br /img srcimage5_thumb.jpg //aa hrefimage6.html nameitemsspanh5test/h5/spanName: My image 6 br /img srcimage6_thumb.jpg //a/div/body
/htmlfrom lxml import etreehtml etree.HTML(doc)
htmletree.parse(search.html,etree.HTMLParser())
1 所有节点
ahtml.xpath(//*)
2 指定节点结果为列表
ahtml.xpath(//head)
3 子节点子孙节点
ahtml.xpath(//div/a)
ahtml.xpath(//body/a) #无数据
ahtml.xpath(//body//a)
4 父节点
ahtml.xpath(//body//a[hrefimage1.html]/..)
ahtml.xpath(//body//a[1]/..)
也可以这样
ahtml.xpath(//body//a[1]/parent::*)
ahtml.xpath(//body//a[1]/parent::div)
5 属性匹配
ahtml.xpath(//body//a[hrefimage1.html])# 6 文本获取 /text()
ahtml.xpath(//body//a[hrefimage1.html]/text())# 7 属性获取 属性名
ahtml.xpath(//body//a/href)
# 注意从1 开始取不是从0
ahtml.xpath(//body//a[1]/href)# 8 属性多值匹配
a 标签有多个class类直接匹配就不可以了需要用contains
ahtml.xpath(//body//a[classli])
ahtml.xpath(//body//a[contains(class,li)])
ahtml.xpath(//body//a[contains(class,li)]/text())
9 多属性匹配
ahtml.xpath(//body//a[contains(class,li) or nameitems])
ahtml.xpath(//body//a[contains(class,li) and nameitems]/text())
ahtml.xpath(//body//a[contains(class,li)]/text())
10 按序选择
ahtml.xpath(//a[2]/text())
ahtml.xpath(//a[2]/href)
取最后一个
ahtml.xpath(//a[last()]/href)
ahtml.xpath(//a[last()-1]/href) # 倒数第二个
位置小于3的
a html.xpath(//a[position()3]/href)# 倒数第三个
ahtml.xpath(//a[last()-2]/href)
11 节点轴选择
ancestor祖先节点
使用了* 获取所有祖先节点
ahtml.xpath(//a/ancestor::*)
# 获取祖先节点中的div
ahtml.xpath(//a/ancestor::div)
attribute属性值
ahtml.xpath(//a[1]/attribute::*)
ahtml.xpath(//a[1]/attribute::href)# child直接子节点
ahtml.xpath(//a[1]/child::*)
descendant所有子孙节点
ahtml.xpath(//a[6]/descendant::*)
following:当前节点之后所有节点
ahtml.xpath(//a[1]/following::*)
ahtml.xpath(//a[1]/following::*[1]/href)
following-sibling:当前节点之后同级节点
ahtml.xpath(//a[1]/following-sibling::*)
ahtml.xpath(//a[1]/following-sibling::a)
ahtml.xpath(//a[1]/following-sibling::*[2])
ahtml.xpath(//a[1]/following-sibling::*[2]/href)# print(a)
/ // . .. 取文本 /text() 取属性 /属性名 根据属性过滤 [属性名属性值] class 特殊 [contains(class,li)]
终极大招 直接复制
案例
import requests resrequests.get(https://www.w3school.com.cn/xpath/xpath_syntax.asp) from lxml import etreehtml etree.HTML(res.text)
ahtml.xpath(//div[idintro]//strong/text())
ahtml.xpath(/html/body/div/div[4]/div[2]/p/strong)
ahtml.xpath(//*[idintro]/p/strong/text()) print(a) 9 动作链
模拟鼠标点住拖动的效果实现滑块认证# 两种形式-形式一actionsActionChains(bro) #拿到动作链对象actions.drag_and_drop(sourse,target) #把动作放到动作链中准备串行执行actions.perform()-方式二ActionChains(bro).click_and_hold(sourse).perform()distancetarget.location[x]-sourse.location[x]track0while track distance:ActionChains(bro).move_by_offset(xoffset2,yoffset0).perform()track2from selenium import webdriver
from selenium.webdriver import ActionChains from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 import time from selenium.webdriver.common.by import Bydriver webdriver.Chrome() driver.get(http://www.runoob.com/try/try.php?filenamejqueryui-api-droppable) driver.implicitly_wait(3) driver.maximize_window()try:driver.switch_to.frame(iframeResult) ##切换到iframeResultsourse driver.find_element(By.ID, draggable)target driver.find_element(By.ID, droppable)# 方式一基于同一个动作链串行执行# actions ActionChains(driver) # 拿到动作链对象# actions.drag_and_drop(sourse, target) # 把动作放到动作链中准备串行执行# actions.perform()# 方式二不同的动作链每次移动的位移都不同ActionChains(driver).click_and_hold(sourse).perform() # 鼠标点中源 标签 不松开distancetarget.location[x]-sourse.location[x]track 0while track distance:ActionChains(driver).move_by_offset(xoffset2, yoffset0).perform()track 2ActionChains(driver).release().perform()time.sleep(10)finally:driver.close() 10 自动登录12306 import time from selenium.webdriver import ActionChains from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options
12306检测到咱们用了自动化测试软件
options Options() options.add_argument(–disable-blink-featuresAutomationControlled) # 去掉自动化控制 bro webdriver.Chrome(chrome_optionsoptions) bro.get(https://kyfw.12306.cn/otn/resources/login.html) bro.implicitly_wait(5) bro.maximize_window() user_login bro.find_element(By.CSS_SELECTOR,#toolbar_Div div.login-panel div.login-box ul li.login-hd-code.active a)user_login.click() time.sleep(1)username bro.find_element(By.ID, J-userName) password bro.find_element(By.ID, J-password) submit_btn bro.find_element(By.ID, J-login) username.send_keys(18953675221) password.send_keys() time.sleep(3) submit_btn.click()time.sleep(5)# 找到滑块 span bro.find_element(By.ID, nc_1_n1z) ActionChains(bro).click_and_hold(span).perform() ActionChains(bro).move_by_offset(xoffset300, yoffset0).perform() ActionChains(bro).release().perform() time.sleep(5)bro.close()
- 上一篇: 道路建设去什么网站能看到wordpress增加访问速度
- 下一篇: 得力企业网站建设网页微博怎么看直播
相关文章
-
道路建设去什么网站能看到wordpress增加访问速度
道路建设去什么网站能看到wordpress增加访问速度
- 技术栈
- 2026年03月21日
-
道滘网站仿做深圳网站设计灵点网络口碑好
道滘网站仿做深圳网站设计灵点网络口碑好
- 技术栈
- 2026年03月21日
-
道滘东莞微信网站建设常州网站建设方案书
道滘东莞微信网站建设常州网站建设方案书
- 技术栈
- 2026年03月21日
-
得力企业网站建设网页微博怎么看直播
得力企业网站建设网页微博怎么看直播
- 技术栈
- 2026年03月21日
-
德宏商城网站建设网站建设公司的公司
德宏商城网站建设网站建设公司的公司
- 技术栈
- 2026年03月21日
-
德化网站建设微信公众号的模板网站
德化网站建设微信公众号的模板网站
- 技术栈
- 2026年03月21日






