1

我之前问过(见这里)如何从 ASPX 表单中抓取结果。表单在新选项卡中呈现输出(通过使用window.openJS 中的函数)。在我之前的帖子中,我没有发出正确的 POST 请求,我解决了这个问题。

以下代码成功地从表单中检索到具有正确请求标头的 HTML 代码,它与我在 Chrome 检查器中看到的 POST 响应完全相同。但是(...)我无法检索数据。用户做出选择后,会打开一个新的弹出窗口,但我无法捕捉到它。弹出窗口有一个新的 URL,其信息不是请求响应正文的一部分。

请求网址:https://apps.neb-one.gc.ca/CommodityStatistics/Statistics.aspx

弹窗网址【我要下载的数据】:https://apps.neb-one.gc.ca/CommodityStatistics/ViewReport.aspx

url = 'https://apps.neb-one.gc.ca/CommodityStatistics/Statistics.aspx'

with requests.Session() as s:
        s.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36",
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "https://apps.neb-one.gc.ca/CommodityStatistics/Statistics.aspx",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "en-US,en;q=0.9"
        }

        response = s.get(url)
        soup = BeautifulSoup(response.content, 'html5lib')

        data = { tag['name']: tag['value'] 
            for tag in soup.select('input[name^=ctl00]') if tag.get('value')
            }
        state = { tag['name']: tag['value'] 
                for tag in soup.select('input[name^=__]')
            }

        payload = data.copy()
        payload.update(state)

        payload.update({
            "ctl00$MainContent$rdoCommoditySystem": "ELEC",
            "ctl00$MainContent$lbReportName": '76',
            "ctl00$MainContent$rdoReportFormat": 'PDF',
            "ctl00$MainContent$ddlStartYear": "2008",
            "__EVENTTARGET": "ctl00$MainContent$rdoCommoditySystem$2"
        })

        print(payload['__EVENTTARGET'])
        print(payload['__VIEWSTATE'][-20:])

        response = s.post(url, data=payload, allow_redirects=True)
        soup = BeautifulSoup(response.content, 'html5lib')

        state = { tag['name']: tag['value'] 
                 for tag in soup.select('input[name^=__]')
             }

        payload.pop("ctl00$MainContent$ddlStartYear")
        payload.update(state)
        payload.update({
            "__EVENTTARGET": "ctl00$MainContent$lbReportName",
            "ctl00$MainContent$lbReportName": "171",
            "ctl00$MainContent$ddlFrom": "01/12/2018 12:00:00 AM"
        })

        print(payload['__EVENTTARGET'])
        print(payload['__VIEWSTATE'][-20:])

        response = s.post(url, data=payload, allow_redirects=True)
        soup = BeautifulSoup(response.content, 'html5lib')

        state = { tag['name']: tag['value']
                 for tag in soup.select('input[name^=__]')
                }

        payload.update(state)
        payload.update({
            "ctl00$MainContent$ddlFrom": "01/10/1990 12:00:00 AM",
            "ctl00$MainContent$rdoReportFormat": "HTML",
            "ctl00$MainContent$btnView": "View"
        })

        print(payload['__VIEWSTATE'])

        response = s.post(url, data=payload, allow_redirects=True)
        print(response.text)

requests有什么方法可以使用和从弹出窗口中检索数据bs4?我注意到它html-requests可以解析和渲染 JS,但我所有的试验都没有成功。

url 源代码显示了这个 JS 代码,我猜它是打开带有数据的弹出窗口的代码:


//<![CDATA[
window.open("ViewReport.aspx", "_blank");Sys.Application.initialize();
//]]>

但我无法访问它。

4

1 回答 1

0

看到这个scrapy博客https://blog.scrapinghub.com/2016/04/20/scrapy-tips-from-the-pros-april-2016-edition

我过去曾使用过这个概念来抓取 aspx 页面。

于 2019-03-01T20:17:55.687 回答