gpt4 book ai didi

ajax - Python2.7 通过请求获取iframe的内容

转载 作者:行者123 更新时间:2023-12-02 03:09:47 26 4
gpt4 key购买 nike

如何通过python的请求得到这个iframe的内容?

在 Firebug 中,iframe 的内容是来自 POST 请求的响应,我发现使用 Python 很难访问它。

代码。

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 请求中添加了浏览器 header 和负载。响应看起来像我想要的(因为它是竖线分隔的)但它不显示 iframe 的内容。

附加代码 - header 和负载。

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>...

Firebug 中的响应包括 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?

感谢阅读。

最佳答案

如果抓取 cookie 然后将其添加到 header ,则 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'}

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

可以通过删除没有值的键来缩短有效负载,例如__事件参数。

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 在每次 session 中都会发生变化,因此需要将它们删除而不是硬编码到程序中。

更新的解决方案

由于某些变量已过期,下面更新的解决方案会在每次运行时抓取新参数。它们是: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 ]

关于ajax - Python2.7 通过请求获取iframe的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40612391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com