Scraping ASPX form and avoiding Selenium

老子叫甜甜 提交于 2020-01-25 09:02:27

问题


I asked previously (see here) how to scrape results from an ASPX form. The form renders the output in a new tab (by using the function window.open in JS). In my previous post, I wasn't making the correct POST request, and I solved that.

The following code successfully retrieves the HTML code from the form with the correct request headers, and it's exactly equal to the POST response I see in the Chrome inspector. But (...) I can't retrieve the data. Once the user make the selections, a new pop-up window opens, but I am not being able to catch it. The pop-up window has a new URL and its information is not part of the request response body.

Request URL: https://apps.neb-one.gc.ca/CommodityStatistics/Statistics.aspx

Pop-up URL [the data I want to download]: 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)

There is any way to retrieve the data from the pop-up window using requests and bs4? I noticed that html-requests can parse and render JS, but all my trials have been unsuccessful.

The url source shows this JS code, which I guess is the one opening the pop-up window with the data:


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

But I'm unable to access to it.


回答1:


See this scrapy blog https://blog.scrapinghub.com/2016/04/20/scrapy-tips-from-the-pros-april-2016-edition

I have used this concept in the past in order to scrape aspx pages.



来源:https://stackoverflow.com/questions/54951057/scraping-aspx-form-and-avoiding-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!