爬虫 selenium

news/2024/7/5 18:24:16

chromedriver下载

谷歌浏览器驱动下载地址:http://chromedriver.storage.googleapis.com/index.html
http://npm.taobao.org/mirrors/chromedriver/

下载的驱动程序必须和浏览器的版本统一,可以根据http://blog.csdn.net/huilan_same/article/details/51896672中提供的版本映射表进行对应
View Code

 

开启浏览器的前端的爬虫

from selenium import webdriver
from time import sleep
bro
= webdriver.Chrome(executable_path=r'D:\爬虫存储\chromedriver.exe')
bro.
get(url='https://www.baidu.com/')
sleep(
2) bro.find_element_by_id('kw').send_keys('python') sleep(1) bro.find_element_by_id('su').click() time.sleep(2)
with open(
'baidu.html', 'w', encoding='utf8') as f: f.write(bro.page_source)
bro.quit()

 

不开启浏览器的前端的爬虫

from selenium.webdriver.chrome.options import Options
chrome_options
= Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu')
url
= 'https://movie.douban.com/typerank?type_name=%E6%83%8A%E6%82%9A&type=19&interval_id=100:90&action=' bro = webdriver.Chrome(executable_path=r'D:\爬虫存储\chromedriver.exe', chrome_options=chrome_options)
bro.
get(url)
bro.execute_script(
'window.scrollTo(0,document.body.scrollHeight)') for i in range(2): sleep(1) bro.execute_script('window.scrollTo(0,document.body.scrollHeight)') sleep(5)
with open(
'douban.html', 'w', encoding='utf8') as f: f.write(bro.page_source)
bro.quit()

 

获取浏览器的实时图片和设置浏览器的大小

from selenium.webdriver.chrome.options import Options

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

url = r'www.baidu.com'

bro = webdriver.Chrome(executable_path=r'D:\爬虫存储\chromedriver.exe', chrome_options=chrome_options)

bro.set_window_size(7680, 4320)
bro.get(url)
sleep(30)
data = bro.get_screenshot_as_png()

with open('1.png', 'wb') as f:
    f.write(data)

bro.quit()

 

在碰到iframe的情况下, 使用选择id等都会找不到, 解决方法

bro.switch_to_frame('login_frame')
bro.find_element_by_id('switcher_plogin').click()
bro.find_element_by_id('u').send_keys('1132300949')
bro.find_element_by_id('login_button').click()
page_text = bro.page_source

 

转载于:https://www.cnblogs.com/NachoLau/p/10453871.html


http://www.niftyadmin.cn/n/1665948.html

相关文章

一次面试总结(记录)

1,从一个数组里找重复出现次数最多的一个数?2,常用的linux命令3.垃圾收集器有哪些 ?垃圾收集算法?4,线上服务器变慢了你是如何定位问题并处理的?5,你自己实现一个本地缓存,淘汰最久未使用,你怎么设计6,用栈实现计算器7,剔除二叉树…

《深入理解计算机系统》笔记-计算机系统漫游

本笔记是学习南大袁春风老师的《计算机系统基础》时整理的笔记。若有理解错误,欢迎大家指正!谢谢~ (该网课是CSAPP的配套资源) 视频地址:https://www.bilibili.com/video/BV1kE411X7S5?p9&spm_id_frompageDriver…

c# winform编程之多线程ui界面资源修改总结篇

单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value "Hello World!";就搞定了,但是如果在一个新线程中这么做,比如: private void btnSet_Click(object sender, EventArgs e) { Th…

selenium 原理应用 - 利用 requests 模拟 selenium 驱动浏览器

前言 selenium 是一个 Web 自动化测试的开源框架,它支持多语言:python/java/c#… 前面也有一篇文章说明了,selenium 浏览器的环境搭建。 selenium 支持多语言,是因为 selenium 与浏览器驱动之间是通过 http 协议进行通信的。只…

区块链与Git版本工具的比较

区块链与Git版本工具的比较 来源:http://www.jianshu.com/p/b96b98983df6作者: 梁波林 相同点: 1. 分布式存储方案 2. 链式数据 3. 去中心化 4. 支持离线访问 5. 交易量较小 6. 参与方多,网络规模大 不同: 1. 数据内容…

RubyMine 2018.3.5 发布,流行的 Ruby 开发工具

开发四年只会写业务代码,分布式高并发都不会还做程序员? >>> RubyMine 2018.3.5 (build 183.5912.16) 发布了,带来了一些平台上的改进。RubyMine 是一个为 Ruby 和 Rails 开发者准备的 IDE。 新版本的更新亮点有: Ruby…

六个开源软件开发的“潜规则”

你想成为开源项目中得意满满、功成名就的那个人吗,那就要遵守下面的“潜规则”。 正如体育界不成文的规定一样,这些规则基本上不会出现在官方文档和正式记录上。比如说,在棒球运动中,从比分领先时不要盗垒,到跑垒员跑了…

C++进阶篇二:C++标准模版库之算法

为什么80%的码农都做不了架构师&#xff1f;>>> 写在<algorithm>头文件之前 在CSTL的头文件<algorithm>中&#xff0c;各式算法函数往往会对支持该算法的最低迭代级别作出要求。任何算法作用的容器或迭代&#xff0c;只有支持的权限等于或高于算法要求…