qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Python Selenium的js擴展實現

            python寫的數據采集,對一般有規律的頁面用 urllib2 + BeautifulSoup + 正則就可以搞定。 但是有些頁面的內容是通過js生成,或者通過js跳轉的,甚至js中還加入幾道混淆機制;對這種涉及頁面腳本解析的內容,前面的方式便很無力。
            這時我們需要能解析、運行js的引擎——瀏覽器,而python selenium能提供程序與瀏覽器的交互接口,再加上phantomjs這個可以后臺運行的瀏覽器,即使用 selenium + phantomjs 便可以解決以上的問題。
            selenium可以操作頁面的元素,并且提供執行js腳本的接口。但其調用js腳本后并不能直接返回執行的結果,這樣再采集內容的過程中就會受到一些限制。 比如我們想使用頁面中的函數進行數據轉換,或者獲取iframe里的內容,這些js產生數據要傳回比較麻煩。
            所以我便寫一個簡化js數據回傳的擴展 exescript.py
          #!/usr/bin/env python
          # -*- coding:utf-8 -*-
          #
          # created by heqingpan
          _init_js="""
          (function (){
          if (window.__e)
          { return;
          }
          var e=document.createElement('div');
          e.setAttribute("id","__s_msg");
          e.style.display="none";
          document.body.appendChild(e);
          window.__e=e;
          })();
          window.__s_set_msg=function(a){
          window.__e.setAttribute("msg",a.toString()||"");
          }
          """
          _loadJsFmt="""
          var script = document.createElement('script');
          script.src = "{0}";
          document.body.appendChild(script);
          """
          _jquery_cdn="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"
          _warpjsfmt="__s_set_msg({0})"
          class ExeJs(object):
          def __init__(self,driver,trytimes=10):
          from time import sleep
          self.driver=driver
          driver.execute_script(_init_js)
          while trytimes >0:
          try:
          self.msgNode=driver.find_element_by_id('__s_msg')
          break
          except Exception:
          sleep(1)
          trytimes -= 1
          if self.msgNode is None:
          raise Exception()
          def exeWrap(self,jsstr):
          """ jsstr 執行后有返回值,返回值通過self.getMsg()獲取 """
          self.driver.execute_script(_warpjsfmt.format(jsstr))
          def loadJs(self,path):
          self.execute(_loadJsFmt.format(path))
          def loadJquery(self,path=_jquery_cdn):
          self.loadJs(path)
          def execute(self,jsstr):
          self.driver.execute_script(jsstr)
          def getMsg(self):
          return self.msgNode.get_attribute('msg')
          打開ipython上一個例子,獲取博客園首頁文章title列表
          from selenium import webdriver
          import exescript
          d=webdriver.PhantomJS("phantomjs")
          d.get("http://www.cnblogs.com/")
          exejs=exescript.ExeJs(d)
          exejs.exeWrap('$(".post_item").length')
          print exejs.getMsg()
          #out:
          """
          20
          """
          jsstr="""(function(){
          var r=[];
          $(".post_item").each(function(){
          var $this=$(this);
          var $h3=$this.find("h3");
          r.push($h3.text());
          });
          return r.join(',');})()"""
          exejs.exeWrap(jsstr)
          l=exejs.getMsg()
          for title in l.split(','):
          print title
          #out:
          """
          mac TeamTalk開發點點滴滴之一——DDLogic框架分解上
          The directfb backend was supported together with linux-fb backend in GTK+2.10
          Science上發表的超贊聚類算法
          功能齊全、效率一流的免費開源數據庫導入導出工具(c#開發,支持SQL server、SQLite、ACCESS三種數據  庫),每月借此處理數據5G以上
          企業級應用框架(三)三層架構之數據訪問層的改進以及測試DOM的發布
          Unity3D 第一季 00 深入理解U3D開發平臺
          Welcome to Swift (蘋果官方Swift文檔初譯與注解二十一)---140~147頁(第三章--集合類型)
          appium簡明教程(11)——使用resource id定位
          SQL語句匯總(終篇)—— 表聯接與聯接查詢
          fopen警告處理方式
          AndroidWear開發之HelloWorld篇
          AMD and CMD are dead之KMD.js版本0.0.2發布
          SQL語句匯總(三)——聚合函數、分組、子查詢及組合查詢
          DevExpress GridControl功能總結
          ASP.NET之Jquery入門級別
          2014年前端面試經歷
          grunt源碼解析:整體運行機制&grunt-cli源碼解析
          跟用戶溝通,問題盡量分析清楚,以及解決問題
          ASP.NET之Ajax系列(一)
          算法復雜度分析
          """

          posted on 2014-09-30 10:03 順其自然EVO 閱讀(1330) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄selenium and watir webdrivers 自動化測試學習

          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 屏东市| 海淀区| 巴青县| 昆山市| 麻江县| 波密县| 武宁县| 崇信县| 佛冈县| 陵水| 乌拉特中旗| 太白县| 丰台区| 潜山县| 延川县| 南溪县| 乌恰县| 同仁县| 遂宁市| 仁寿县| 柳州市| 汉川市| 武义县| 克什克腾旗| 东光县| 尼玛县| 岳西县| 红桥区| 洛川县| 成武县| 东山县| 合阳县| 安阳县| 克拉玛依市| 奉化市| 玛多县| 闽清县| 汤阴县| 织金县| 时尚| 乌拉特前旗|