2016-11-15 397 views
2

如何使用python的請求獲取此iframe的內容?Python2.7獲取iframe的內容請求

在Firebug中,iframe的內容來自我發現使用Python很難訪問的POST請求的響應。

代碼。

import requests 

iframe_url = "https://wwwrs.resbank.co.za/BpsReports/InstNotice.aspx" 

# How do I scrape the payload for the POST request? 
r = requests.post(iframe_url) # data=? 

r.text 

的響應。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head><title>Bank Institutions</title>... 

我想響應。

1|#||4|224|updatePanel|ctl00_MainContent_ReportViewer1_DocMap|<div id="ctl00_MainContent_ReportViewer1_ctl09" style="display:none;"> 
<input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl09$ClientClickedId" id="ctl00_MainContent_ReportViewer1_ctl09_ClientClickedId" /></div>... 

如何抓取POST請求的數據?

編輯。

我在POST請求中添加了瀏覽器標頭和有效載荷。響應看起來像我想要的(因爲它是管道分隔的),但它不顯示iframe的內容。

附加代碼 - 標頭和有效負載。

headers = { 
    'User-agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1', 
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' 
} 

payload = { 
    "ctl00$MainContent$ScriptManager1": "ctl00$MainContent$ScriptManager1|ctl00$MainContent$ReportViewer1$ctl10$Reserved_AsyncLoadTarget", 
    "__EVENTTARGET": "ctl00$MainContent$ReportViewer1$ctl10$Reserved_AsyncLoadTarget", 
    "__EVENTARGUMENT": "", 
    "__VIEWSTATE": 
    "/wEPDwUKLTYyOTE1Njg2OQ9kFgJmD2QWA..." # shortened 
    "__VIEWSTATEGENERATOR": "E6F8CED1", 
    "__EVENTVALIDATION":  
    "/wEdABKvYwmAyl699cXAjRyhDjs8urvg...", # shortened 
    "ctl00$MainContent$ReportViewer1$ctl03$ctl11":"ltr", 
    "ctl00$MainContent$ReportViewer1$ctl03$ctl12":"standards", 
    #"ctl00$MainContent$ReportViewer1$AsyncWait$HiddenCancelField":"False", 
    "ctl00$MainContent$ReportViewer1$ctlToggleParam$store":"false", 
    #"ctl00$MainContent$ReportViewer1$ctl08$collapse":"false", 
    "ctl00$MainContent$ReportViewer1$ctl10$VisibilityState$ctl00":None, 
    #"ctl00$MainContent$ReportViewer1$ctl10$ReportControl$ctl04":100, 
    "__ASYNCPOST": "true" 
} 

r = requests.post(url, headers=headers, data=payload, timeout=3.5) 

幾個按鍵沒有價值和幾個鍵值對中導致500服務器錯誤,所以我省略了他們,並分別評價出來的。

Python中的響應不包括iframe。

1|#||4|7645|updatePanel|ctl00_MainContent_ReportViewer1_ReportViewer| 
    <div id="ctl00_MainContent_ReportViewer1" 
    onclick="if 
    ($get('ctl00_MainContent_ReportViewer1_ctl04') != null && 
    $get('ctl00_MainContent_ReportViewer1_ctl04').control != null) 
    $get('ctl00_MainContent_ReportViewer1_ctl04').control.HideActiveDropDown();" 
    onactivate="if 
    ($get('ctl00_MainContent_ReportViewer1_ctl04') != null && 
    $get('ctl00_MainContent_ReportViewer1_ctl04').control != null) 
    $get('ctl00_MainContent_ReportViewer1_ctl04').control.HideActiveDropDown();" style="height:400px;width:400px;position:absolute;left:0px;Top:0px"> 
    <div id="ctl00_MainContent_ReportViewer1_HttpHandlerMissingErrorMessage" style="border-color:Red;border-width:2px;border-style:Solid;padding:10px;display:none;overflow:auto;font-size:.85em;"> 
    <h2> 
     Report Viewer Configuration Error 
    </h2><p>The Report Viewer Web Control HTTP Handler has not been registered in the application's web.config file. 
      Add <add verb="*" path="Reserved.ReportViewerWebControl.axd" 
      type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, 
      Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> to the system.web/httpHandlers section of the web.config file, or add <add 
      name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" 
      type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, 
      Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> to the system.webServer/handlers section for Internet Information Services 7 or later. 
     </p> 
    </div> 
    <span id="ctl00_MainContent_ReportViewer1_ctl03"> 
    <input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl03$ctl00" id="ctl00_MainContent_ReportViewer1_ctl03_ctl00" /> 
    <input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl03$ctl01" id="ctl00_MainContent_ReportViewer1_ctl03_ctl01" /> 
    </span> 
    <input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl11" id="ctl00_MainContent_ReportViewer1_ctl11" /> 
    <input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl12" id="ctl00_MainContent_ReportViewer1_ctl12" /> 
     <div id="ctl00_MainContent_ReportViewer1_AsyncWait" style="background-color:White;opacity:0.7;position:absolute;display:none;filter:alpha(opacity=70);"> 
     </div> 
     <div id="ctl00_MainContent_ReportViewer1_AsyncWait_Wait" style="cursor:wait;background-color:#ECE9D8;padding:15px;border:1px solid black;display:none;position:absolute;"> 
     <table height="100%"> 
      <tr> 
      <td width="32px" height="32px"> 
       <img src="/BpsReports/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.1&Name=Microsoft.Reporting.WebForms.Icons.SpinningWheel.gif" style="height:32px;width:32px;" /> 
      </td> 
      <td style="vertical-align:middle;text-align:center;"> 
       <span style="font-family:Verdana;font-size:14pt;">Loading...</span> 
       <div style="margin-top:3px;"> 
        <a href="javascript:$get('ctl00_MainContent_ReportViewer1_AsyncWait').control._cancelCurrentPostback();" style="font-family:Verdana;font-size:8pt;color:#3366CC;">Cancel</a>... 

螢火蟲該響應包括的IFRAME。

1|#||4|224|updatePanel|ctl00_MainContent_ReportViewer1_DocMap|<div id="ctl00_MainContent_ReportViewer1_ctl09" 
style="display:none;"> 
    <input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl09$ClientClickedId" id="ctl00_MainContent_ReportViewer1_ctl09_ClientClickedId" 
/> 
</div>|5472|updatePanel|ctl00_MainContent_ReportViewer1_ctl10_ReportArea|<div NewContentType="Microsoft 
.Reporting.WebFormsClient.ReportAreaContent.ReportPage" ForNonReportContentArea="false" id="ctl00_MainContent_ReportViewer1_ctl10_VisibilityState" 
style="visibility:none;"> 
    <input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl10$VisibilityState$ctl00" value="ReportPage" 
/> 
</div><input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl10$ScrollPosition" id="ctl00_MainContent_ReportViewer1_ctl10_ScrollPosition" 
/><span id="ctl00_MainContent_ReportViewer1_ctl10_Reserved_AsyncLoadTarget"></span><div id="ctl00_MainContent_ReportViewer1_ctl10_ReportControl" 
style="display:none;"> 
    <span></span><input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl10$ReportControl$ctl02" id 
="ctl00_MainContent_ReportViewer1_ctl10_ReportControl_ctl02" /><input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl10$ReportControl$ctl03" 
id="ctl00_MainContent_ReportViewer1_ctl10_ReportControl_ctl03" /><input type="hidden" name="ctl00$MainContent$ReportViewer1$ctl10$ReportControl$ctl04" 
id="ctl00_MainContent_ReportViewer1_ctl10_ReportControl_ctl04" value="100" /><div style="display:none 
;"> 
     <DIV dir=...The content of the iframe. 

問題可以輔助的溶液。

  1. 如何颳了POST請求的負載?

  2. 是請求準備有效負載的順序?

  3. 如何獲取iframe的請求?

感謝您的閱讀。

+0

您還沒有提供足夠的信息......您在螢火蟲中看到的請求的確切順序以及它在POST請求中包含的額外數據是什麼? –

+0

總共有11個請求(包括請求圖像)兩個請求使用相同的URL,其中第二個請求包含iframe的內容。我可以包含一個屏幕快照,顯示Firebug的請求順序,如果這會有所幫助的話。如何找出POST請求中包含的額外數據?這是Firebug - Net - Post中的參數嗎? – Astrophe

回答

1

iframe的內容在cookie被抓取並添加到標頭時可用。

headers = {'User-agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'} 

r = requests.get(url) 
cookie = {'Cookie': r.headers['Set-Cookie']} 
headers.update(cookie) 

可以通過刪除沒有值的鍵來縮短有效載荷,例如, __EVENTARGUMENT。

payload = { 
    "__EVENTTARGET": "ctl00$MainContent$ReportViewer1$ctl10$Reserved_AsyncLoadTarget", 
    "__VIEWSTATE": "/wEPDwUKLTYyOTE1Njg2OQ9kFgJmD2...", # shortened 
    "__EVENTVALIDATION": "/wEdABKvYwmAyl699cXAjRyh...", # shortened 
    "__ASYNCPOST": "true" 
} 

iframe(URL)的內容可以存儲在列表中。

from bs4 import BeautifulSoup 
soup = BeautifulSoup(r.text) 
links = soup.table.find_all('a') 
hrefs = [ i['href'] for i in links ] 

>>>['https://wwwrs.resbank.co.za/BpsReports/bankContactRprtPage.**Edit.**aspx?Code=BL', 'https://wwwrs.resbank.co.za/BpsReports/bankContactRprtPage.aspx?Code=BR', 'https://wwwrs.resbank.co.za/BpsReports/bankContactRprtPage.aspx?Code=R', 'https://wwwrs.resbank.co.za/BpsReports/bankContactRprtPage.aspx?Code=FB', 'https://wwwrs.resbank.co.za/BpsReports/bankContactRprtPage.aspx?Code=LB', 'https://wwwrs.resbank.co.za/BpsReports/bankContactRprtPage.aspx?Code=MB'] 

更新

這個答案很短的時間段後失敗。我認爲這是因爲參數__VIEWSTATE和__ EVENTVALIDATION在每個會話中都會發生變化,所以他們需要被抓取而不是被硬編碼到程序中。

更新的解決方案

由於一些變量的到期,下面擦傷新的參數更新的解決方案每次運行。這些是:Cookie,__VIEWSTATE和__EVENTVALIDATION。這現在是一個工作解決方案。

import requests 
from bs4 import BeautifulSoup 
import re 

url = "https://wwwrs.resbank.co.za/BpsReports/InstNotice.aspx" 

r = requests.get(url) 

headers = {'User-agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) '\ 
    + 'Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'} 

cookie = {'Cookie': r.headers['Set-Cookie']} 
headers.update(cookie) 

payload = { 
    '__EVENTTARGET': 'ctl00$MainContent$ReportViewer1$ctl10$Reserved_AsyncLoadTarget', 
    '__ASYNCPOST': 'true' 
} 

view_state = re.search(r'__VIEWSTATE"\svalue="(.*)"', r.text) 
payload['__VIEWSTATE'] = str(view_state.group(1)) 
# request docs advise str over unicode for header values. 

event_validation = re.search(r'__EVENTVALIDATION"\svalue="(.*)"', r.text) 
payload['__EVENTVALIDATION'] = str(event_validation.group(1)) 

r = requests.post(url, headers=headers, data=payload, timeout=3.5) 

soup = BeautifulSoup(r.text) 
links = soup.table.find_all('a') 
hrefs = [ i['href'] for i in links ]