2017-02-22 27 views
0

我是一個熱愛計算器的人,通常通過這個網站找到我的問題的解決方案。但是,下面的問題已經困擾了我這麼長時間,它迫使我在這裏創建一個帳戶,並直接問:動態內容的Python抓取(視覺效果與html源代碼不同)

我想景觀此鏈接:https://permid.org/1-21475776041我要的是該行「TRCS資產類別 「和」貨幣「。

對於初學者來說,我使用這個代碼:

from bs4 import BeautifulSoup 
import urllib2 

url = 'https://permid.org/1-21475776041' 

req = urllib2.urlopen(url) 
raw = req.read() 
soup = BeautifulSoup(raw) 
print soup.prettify() 

HTML代碼返回(見下文)是什麼,你可以在你的瀏覽器在點擊鏈接查看不同:

<!DOCTYPE html> 
<!--[if lt IE 7]>  <html ng-app="tmsMdaasApp" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> 
<!--[if IE 7]>   <html ng-app="tmsMdaasApp" class="no-js lt-ie9 lt-ie8"> <![endif]--> 
<!--[if IE 8]>   <html ng-app="tmsMdaasApp" class="no-js lt-ie9"> <![endif]--> 
<!--[if gt IE 8]><!--> 
<html class="no-js" ng-app="tmsMdaasApp"> 
<!--<![endif]--> 
<head> 
    <meta content="text/html; charset=utf-8" http-equiv="content-type"/> 
    <meta charset="utf-8"/> 
    <meta content="ie=edge" http-equiv="x-ua-compatible"/> 
    <meta content="max-age=0,no-cache" http-equiv="Cache-Control"/> 
    <base href="/"/> 
    <title ng-bind="PageTitle"> 
    Thomson Reuters | PermID 
    </title> 
    <meta content="" name="description"/> 
    <meta content="width=device-width, initial-scale=1" name="viewport"/> 
    <meta content="#ff8000" name="theme-color"/> 
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory --> 
    <link href="app/vendor.daf96efe.css" rel="stylesheet"/> 
    <link href="app/app.1405210f.css" rel="stylesheet"/> 
    <link href="favicon.ico" rel="icon"/> 
    <!-- Typekit --> 
    <script src="//use.typekit.net/gnw2rmh.js"> 
    </script> 
    <script> 
    try{Typekit.load({async:true});}catch(e){} 
    </script> 
    <!-- // Typekit --> 
    <!-- Google Tag Manager Data Layer --> 
    <!--<script> 
     analyticsEvent = function() {}; 
     analyticsSocial = function() {}; 
     analyticsForm = function() {}; 
     dataLayer = []; 
    </script>--> 
    <!-- // Google Tag Manager Data Layer --> 
</head> 
<body class="theme-grey" id="top" ng-esc=""> 
    <!--[if lt IE 7]> 
     <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> 
    <![endif]--> 
    <!-- Add your site or application content here --> 
    <navbar class="tms-navbar"> 
    </navbar> 
    <div id="body" role="main" ui-view=""> 
    </div> 
    <div id="footer-wrapper" ng-show="!params.elementsToHide"> 
    <footer id="main-footer"> 
    </footer> 
    </div> 
    <!--[if lt IE 9]> 
    <script src="bower_components/es5-shim/es5-shim.js"></script> 
    <script src="bower_components/json3/lib/json3.min.js"></script> 
    <![endif]--> 
    <script src="app/vendor.8cc12370.js"> 
    </script> 
    <script src="app/app.6e5f6ce8.js"> 
    </script> 
</body> 
</html> 

有沒有人知道我在這裏錯過了什麼,以及我如何才能讓它工作?

+1

要渲染動態內容你會需要使用瀏覽器。檢查Selenium做到這一點。 –

+0

[用Python抓取JavaScript網頁]可能的重複(http://stackoverflow.com/questions/8049520/web-scraping-javascript-page-with-python) –

回答

1

感謝,Teemu Risikko - 你鏈接的網站的評論(雖然不是解決)讓我在正確的道路上。

萬一是別人碰到了同樣的問題,這裏是我的解決方案:我通過傳統的「刮痧」(例如BeautifulSoup或LXML)獲得通過請求數據並沒有。

  1. 使用Google Chrome導航至website
  2. 右鍵單擊網站並選擇「檢查」。
  3. 在頂部導航欄上選擇「網絡」。
  4. 將網絡監視器限制爲「XHR」。
  5. 其中一個條目(帶有箭頭的市場)顯示可以與請求庫一起使用的鏈接。

Screenshot

import requests 
url = 'https://permid.org/api/mdaas/getEntityById/21475776041' 
headers = {'X-AG-Access-Token': YOUR_ACCESS_TOKEN} 
r = requests.get(url, headers=headers) 
r.json() 

它得到我:

{u'Asset Class': [u'Units'], 
u'Asset Class URL': [u'https://permid.org/1-302043'], 
u'Currency': [u'CAD'], 
u'Currency URL': [u'https://permid.org/1-500140'], 
u'Exchange': [u'TOR'], 
u'IsQuoteOf.mdaas': [{u'Is Quote Of': [u'Convertible Debentures Income Units'], 
    u'URL': [u'https://permid.org/1-21475768667'], 
    u'quoteOfInstrument': [u'21475768667'], 
    u'quoteOfInstrument URL': [u'https://permid.org/1-21475768667']}], 
u'Mic': [u'XTSE'], 
u'PERM ID': [u'21475776041'], 
u'Quote Name': [u'CONVERTIBLE DEBENTURES INCOME UNT'], 
u'Quote Type': [u'equity'], 
u'RIC': [u'OCV_u.TO'], 
u'Ticker': [u'OCV.UN'], 
u'entityType': [u'Quote']} 
0

使用具有大量頁面的默認用戶代理將爲您提供不同的外觀頁面,因爲它使用的是過時的用戶代理。這是你的輸出告訴你的。

Reference on Changing user-agents

思想,這可能是你的問題,它並不完全回答關於在網頁上得到應用動態變化的問題。要獲取動態更改的數據,您需要模擬頁面加載時的JavaScript請求。如果你提出JavaScript的請求,你會得到JavaScript獲取的數據。