当前位置: 首页 > news >正文

python 爬虫 selenium 笔记

todo
  1. 阅读并熟悉 Xpath, 这个与 Selenium 密切相关、
selenium
  1. selenium 加入无图模式,速度快很多。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options# selenium 无图模式,速度快很多。
option = Options()
option.page_load_strategy = "none"
prefs = {"profile.managed_default_content_settings.images": 2}  # 设置无图模式
option.add_experimental_option("prefs", prefs)  # 加载无图模式设置driver = webdriver.Chrome(chrome_options=option)
  1. 遇到 BeautifulSoup iframe
  • 一种解决方案是, 获得iframe的src属性,然后请求并解析其内容:
  • 另一种是:
driver.get(url)
iframe = driver.find_elements_by_tag_name('iframe')[1]
driver.switch_to.frame(iframe) # 最重要的一步
soup = BeautifulSoup(driver.page_source, "html.parser")
个人常犯的错误, 误区,陷阱
  1. driver.execute_script(JS) 这个才是执行 JS,
    注意是 execute_script, 不是 execute。
页面等待。这个是比较关键的。
  1. 显式等待。貌似比较麻烦,且不常用。
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
  1. 隐式等待。推荐使用。

driver.implicitly_wait(10) # seconds

定位元素
  1. 定位元素之前,加上这句话,笔记安全。

bot.implicitly_wait(10) # 这句话很关键。

  1. 查找元素的方法
find_element_by_id()
find_element_by_name()              # 这个name 是标签里面的一种属性。
find_element_by_xpath()             
find_element_by_link_text()         # 比如  'Sign In'
find_element_by_partial_link_tex()      
find_element_by_tag_name()
find_element_by_class_name()
find_element_by_css_selector()
基本配置,导包
import os
import random
import json
import pickle
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import pyautogui as pt
import pyperclip
切换frame
  1. 遇到 iframe,最好是切换过去, 见 https://blog.csdn.net/huilan_same/article/details/52200586

driver.switch_to.frame(0) # 1.用frame的index来定位,第一个是0

点击元素。不可点击的元素, 执行下面的方法。
def real_click(self, driver, ele):actions = ActionChains(driver)actions.move_to_element(ele)actions.click(ele)actions.perform()
执行 js, 页面滚动
# 先滚动到底部,然后再滚动到顶部
# window.scrollTo(0,document.body.scrollHeight);js = "var q=document.documentElement.scrollTop=500"
bot.execute_script(js)js2 = "document.body.scrollTop=document.documentElement.scrollTop=0;"
bot.execute_script(js2)
填写表格。这个需要再读读看。
element = driver.find_element_by_xpath("//select[@name='name']")
choices = element.find_elements_by_tag_name("option")
for c in choices:print("Value is: %s" % c.get_attribute("value"))c.click()
封装一些自己常用的方法
@staticmethod
def save_html(bot):             # 保存 htmlfilename = 'ret.html'data = bot.page_sourcewith open(filename, 'w') as f:f.write(data)print("保存 html 完成!")@staticmethod
def real_click(driver, ele):    # 点击元素actions = ActionChains(driver)actions.move_to_element(ele)actions.click(ele)actions.perform()@staticmethod
def send_word(ele, word):       # 输入框,输入文字ele.clear()ele.send_keys(word)ele.send_keys(Keys.RETURN)
源码中有趣的,有用的方法

Driver

  1. driver.current_url # 本身就是静态方法
  2. driver.page_source
  3. driver.save_screenshot(‘foo.png’)
  4. driver.get_log(‘driver’)
  5. driver.page_source # 保存 html 源码,功本地调试,减少网络请求
  6. driver.title 直接获取页面的标题, 很适合作为文件名。

WebElement

  1. ele.id # 直接就可以用
  2. ele.get_attribute(“class”) # 这个很常用的。

个人接单,python, R语言,有事请私聊

老哥,支持一下啊。

支付宝扫码领红包哦

http://www.lryc.cn/news/441757.html

相关文章:

  • git分支管理的一些常用规范
  • GPT-4论文阅读
  • this 指向
  • 【贪心算法】贪心算法一
  • windnd.hook_dropfiles中的create_buffer值太小无法拖放长文件名
  • Gitlab runner的使用示例(二):Maven + Docker 自动化构建与部署
  • QNX Hypervisor(十)Linux Guest IPC 二
  • 怎样把PPT上顽固的图标删了
  • 【论文阅读】Slim Fly: A Cost Effective Low-Diameter Network Topology 一种经济高效的小直径网络拓扑
  • Prometheus使用Pushgateway推送数据
  • 【Oracle】调优与oracle最大连接数配置
  • Unity教程(十六)敌人攻击状态的实现
  • ​图像超分辨率(ISR)​
  • 园区网基础组网保姆级(mstp,vrrp,irf,eth-trunk,route-policy,ospf,bgp,rbm,nat,mlag等等)
  • 大数据技术原理与应用
  • 《黑神话悟空》开发框架与战斗系统解析
  • 网络资源模板--Android Studio 通讯录App
  • Spring 出现 No qualifying bean of type ‘com.xxx‘ available 解决方法
  • C# 批量更改文件后缀名称
  • KIC算法介绍及pyrosetta示例代码
  • 【论文串烧】多媒体推荐中的模态平衡学习 | 音视频语音识别中丢失导致的模态偏差对丢失视频帧鲁棒性的影响
  • 【C语言二级考试】循环结构设计
  • 诗文发布模板(python代码打造键盘录入诗文自动排版,MarkDown源码文本)
  • GO主流开源框架
  • LeetCode:2398. 预算内的最多机器人数目 双指针+单调队列,时间复杂度O(n)
  • oracle 插入date日期类型的数据、插入从表中查出的数据,使用表中的默认数据
  • 物流系统打单软件 佳易王物流运单怎么打印教程
  • 二叉树计算
  • Java并发执行举例
  • Java 基础知识九(网络编程)