动态网页解析 Selenium

转自:http://blog.csdn.net/dreamd1987/article/details/8202111?reload


对于静态网页的解析,我们一般使用Jsoup就可以了

但是对已动态加载的网页,Jsoup就不可以了!

那么我们如何解析并抓取网页上的信息呢?

看了网上朋友的讨论,我打算模拟一个浏览器然后通过操作浏览器来得到新的网页信息。

最终我选择了Selenium来模拟浏览器。

其实Selenium是一个测试浏览器性能的工具,用来爬虫有点大材小用了!

Selenium官网地址:http://seleniumhq.org/

大家可以去官网上产科selenium的安装和使用

我们一般使用Selenium RC的工具包来对浏览器进行操作。

安装好包后,我们给出一个小例子:

[html]  view plain copy
  1. package com.example.tests;  
  2. // We specify the package of our tests  
  3.   
  4. import com.thoughtworks.selenium.*;  
  5. // This is the driver's import. You'll use this for instantiating a  
  6. // browser and making it do what you need.  
  7.   
  8. import java.util.regex.Pattern;  
  9. // Selenium-IDE add the Pattern module because it's sometimes used for  
  10. // regex validations. You can remove the module if it's not used in your  
  11. // script.  
  12.   
  13. public class NewTest extends SeleneseTestCase {  
  14. // We create our Selenium test case  
  15.   
  16.       public void setUp() throws Exception {  
  17.         setUp("http://www.google.com/", "*firefox");  
  18.              // We instantiate and start the browser  
  19.       }  
  20.   
  21.       public void testNew() throws Exception {  
  22.            selenium.open("/");  
  23.            selenium.type("q", "selenium rc");  
  24.            selenium.click("btnG");  
  25.            selenium.waitForPageToLoad("30000");  
  26.            assertTrue(selenium.isTextPresent("Results * for selenium rc"));  
  27.            // These are the real test steps  
  28.      }  
  29. }  

这个是使用的类。

我们可以编写一个主程序,如下:

[html]  view plain copy
  1. package Test1;  
  2.   
  3. import java.net.UnknownHostException;  
  4.   
  5. import com.mongodb.BasicDBObject;  
  6. import com.thoughtworks.selenium.*;  
  7. //This is the driver's import. You'll use this for instantiating a  
  8. //browser and making it do what you need.  
  9. import org.jsoup.Jsoup;  
  10. import org.jsoup.helper.Validate;  
  11. import org.jsoup.nodes.Document;  
  12. import org.jsoup.nodes.Element;  
  13. import org.jsoup.select.Elements;  
  14.   
  15. import java.util.LinkedList;  
  16. import java.util.Queue;  
  17. import java.util.regex.Pattern;  
  18. //Selenium-IDE add the Pattern module because it's sometimes used for  
  19. //regex validations. You can remove the module if it's not used in your  
  20. //script.  
  21.   
  22. @SuppressWarnings("deprecation")  
  23. public class NewTest extends SeleneseTestCase {  
  24. //We create our Selenium test case  
  25.   
  26.     public String url;  
  27.    public void setUp() throws Exception {  
  28.      setUp("https://foursquare.com/v/singapore-zoo/4b05880ef964a520b8ae22e3", "*chrome");  
  29.      //selenium.waitForPageToLoad("30000");  
  30.           // We instantiate and start the browser  
  31.    }  
  32. public void testNew() throws Exception {  
  33.           
  34.         selenium.open("https://foursquare.com/v/singapore-zoo/4b05880ef964a520b8ae22e3");  
  35.         selenium.windowMaximize();  
  36. public static void print(String msg, Object... args) {  
  37.            System.out.println(String.format(msg, args));  
  38.        }  
  39.        public static void gettips(Document doc){  
  40.              
  41.            Elements tips = doc.select(".tipText");  
  42.             int count = 0;  
  43.             //BasicDBObject document4 = new BasicDBObject();  
  44.             for (Element link : tips){  
  45.                   
  46.                 String str2 = new String(link.text());  
  47.                 count++;  
  48.                 String tempint = String.valueOf(count);  
  49.                 //document4.put(tempint, str);  
  50.                 print("%s \r\n", str2);  
  51.             }  
  52.        }  
  53. }  

运行后,程序会打开firefox的浏览器,然后会自动操作你对他的设计。

具体的API可以去这里查看:

http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/

同时,既然我们需要抓取网页,那么我们一定需要了解Xpath,因为selenium中需要找寻button等按钮等都要用到Xpath的选择模式。

之后仔细研究后我会争取写一篇Xpath的使用方式,其实网上的教程也很多。

同样,如何大家不习惯Selenium的找寻模式。可以按照以下的代码:

[html]  view plain copy
  1. String str = selenium.getHtmlSource();  
  2. //get the source of html for the web.  
  3. doc = Jsoup.parse(str);  

第一行代码抓取当前网页的html代码

的二行代码将html代码转换冲了jsoup可以读取的doc变量。

之后就可以使用大家习惯的Jsoup去解析网页了。

不过这样做有点浪费,因为selenium已经可以完全解析了。

同样selenium可以模拟登陆操作等。

总之你在浏览器上可以做的工作,selenium都可以替你完成。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值