编写不易,转载请注明(http://shihlei.iteye.com/blog/2067716)!
一概述
获取动态资源,可以使用HtmlUnit,但是其对JS的支持还是不够完善。相对与HtmlUnit还有一种驱动浏览器的下载还原工具Selenium。可以打开浏览器,获取网页,下载解析,支持dom,js,解析效果更好,但是打开浏览器速度方面有一定损失。个人实验,禁用CSS,图片下载,速度还尚可。
Selenium也是自动化测试工具,支持驱动不同的浏览器,Firefox,IE,Chrome等,也包含HtmlUnit提供的驱动实现。
本文描述Selenium驱动Firefox,请求响应,设置cookies,驱动JS等方法,用他登录某主流weibo还是不错的。
二 版本
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.41.0</version> </dependency>
三 典型应用
1)打开Google,搜索baidu
可以看到Firefox从打开,填写表单,到提交打开新页面过程。
/** * 打开google搜索百度 * * @param queryStr */ public static void demo() { String url = "http://www.google.com.hk"; WebDriver webDriver = new FirefoxDriver(); // 打开google webDriver.get(url); // 使用Selenium的dom模型获取form WebElement webElement = webDriver.findElement(By.name("q")); webElement.sendKeys("baidu"); webElement.submit(); // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(webDriver, 100)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().indexOf("baidu") != -1; } }); String responseBody = webDriver.getPageSource(); System.out.println("Response : " + responseBody); // 关闭浏览器 webDriver.close(); }
2)获取动态网页
与下面的请求响应一样,打开页面等待加载完毕即可,JS填充页面,AJAX都OK。
四 样例
(1)请求响应
public static void main(String[] args) throws Exception { String url = "http://www.google.com"; WebDriver webDriver = new FirefoxDriver(); // 打开google webDriver.get(url); String responseBody = webDriver.getPageSource(); System.out.println("Response : " + responseBody); webDriver.quit(); }
(2)配置不加载资源
/** * 获得不加载 css,图片,flash的浏览器 * @return */ public WebDriver getNoResouceWebDriver(){ FirefoxProfile firefoxProfile = new FirefoxProfile(); // 去掉css firefoxProfile.setPreference("permissions.default.stylesheet", 2); // 去掉图片 firefoxProfile.setPreference("permissions.default.image", 2); // 去掉flash firefoxProfile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", false); return new FirefoxDriver(firefoxProfile); }
(3)配置Firefox路径
启动报找不到Firefox的时候可以使用:System.setProperty("webdriver.firefox.bin", firefoxPath)
(4)Cookies
1)设置Cookies
public void setCookies(WebDriver,webDriver,Map<String, String> cookies) { if (cookies != null && cookies.size() > 0) { for (Entry<String, String> c : cookies.entrySet()) { Cookie cookie = new Cookie(c.getKey(), c.getValue()); webDriver.manage().addCookie(cookie); System.out.println("Set Cookies : " + c.getKey() + " | " + c.getValue()); } } }
2)获取响应Cookies
public Map<String,String> getCookies(WebDriver webDriver){ Set<Cookie> cookies = webDriver.manage().getCookies(); Map<String, String> responseCookies = new HashMap<String,String>(); for (Cookie c : cookies) { responseCookies.put(c.getName(), c.getValue()); } return responseCookies; }
3)清理Cookies
/** * 清除所有cookie */ public void clearCookies(WebDriver webDriver) { webDriver.manage().deleteAllCookies(); }
(5)驱动JS
Selenium 的Dom对不可见的Element(html有但是CSS属性为不显示等)找不到,这时候使用JS操作和提交是个不错的选择:
public void doWeb(WebDriver webDriver) { StringBuilder js = new StringBuilder(); js.append("document.getElementsByName('username')[1].value='").append(WeiboAccount.USERNAME) .append("';"); js.append("document.getElementsByName('password')[1].value='").append(WeiboAccount.PASSWORD) .append("';"); js.append("document.getElementsByClassName('W_btn_g')[1].click();"); ((JavascriptExecutor) webDriver).executeScript(js.toString()); (new WebDriverWait(webDriver, 100)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().indexOf("我的首页") != -1; } }); }
相关推荐
selenium-java-2.41.0
selenium-dotnet-2.41.0
selenium.jar selenium资源包
selenium-java-2.41.0,selenium-java-2.33 完整包下载
SELENIUM获取网址.pySELENIUM获取网址.pySELENIUM获取网址.pySELENIUM获取网址.py
selenium webdriver 爬虫爬取动态网页,里面有chromedriver,windows平台可用,linux需要更换linux的chromedriver。修改代码可爬取其他网站的
python获取html动态生成的数 python获取html动态生成的数
selenium方法获取静态页面数据方法 from selenium import webdriver import time opt = webdriver.ChromeOptions() opt.set_headless() driver =webdriver.Chrome(options=opt) while True: print('页数:',page...
py依赖包
方法一: 根据table的id属性和table中的某一个元素定位其在table中的位置 table包括表头,位置坐标都是从1开始算 tableId:table的id属性 queryContent:需要确定位置的内容 def get_table_content(tableId,...
selenium自动化测试,java开发用jar包。版本有点老,大家要是能用上就拿去用吧。selenium自动化测试,java开发用jar包。版本有点老,大家要是能用上就拿去用吧。selenium自动化测试,java开发用jar包。版本有点老,...
虽然scrapy能够完美且快速的抓取静态页面,但是在现实中,目前绝大多数网站的页面都是动态页面,动态页面中的部分内容是浏览器运行页面中的JavaScript脚本动态生成的,爬取相对困难; 比如你信心满满的写好了一个...
通过Selenium获取淘宝订单信息,内容: 一、登录方式:1、通过config.ini配置设置账号密码自动登录 2、通过扫码自动登录 二、数据库存储,可通过config.ini配置sqlserver数据库连接保存
相关介绍 Python是一种跨平台的计算机程序设计语言。...Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,GoogleChrome,Opera,Edge等。
selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium
Python 通过Selenium浏览器自动化测试框架获取HTML代码中的可用数据 Python源码Python 通过Selenium浏览器自动化测试框架获取HTML代码中的可用数据 Python源码Python 通过Selenium浏览器自动化测试框架获取HTML代码...
python selenium 获取接口数据。 selenium没有直接提供查询的函数,但是可以通过webdriver提供的API查询,使用的函数是Network.getResponseBody webdriver提供的API文档:...
Selenium 模拟浏览器动态加载页面的实现方法
from selenium import webdriver from time import sleep sleep(2) driver = webdriver.Chrome() driver.get(https://www.baidu.com/) # 移动浏览器观看展示 driver.set_window_size(width=500, heig