2016-11-15 400 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 ]