【问题标题】:Screen scraping site to new browser window屏幕抓取网站到新的浏览器窗口
【发布时间】:2018-05-24 06:56:50
【问题描述】:

我不确定我的要求是否可行。我有内部 Sharepoint 站点,只能通过有效凭据(Windows/SQL 身份验证)访问。

从测试 ASP.NET Web 应用程序中,我执行了登录屏幕抓取并获取 _VIEWSTATE、__VIEWSTATEGENERATOR、__EVENTVALIDATION 等,然后提供凭据和准备好的 PostData,并能够获取 cookie,最终能够成功登录到安全页面。我得到了安全页面的 html 源代码。

现在,我需要在单独的浏览器窗口中启动它,以便安全页面按原样打开,然后所有页面链接都可以正常工作。

当我编写Response.Write(responseData); 时,页面源代码(html) 在当前测试 ASP.NET Web 应用程序中打开。

有什么建议吗?

【问题讨论】:

    标签: asp.net screen-scraping


    【解决方案1】:

    也许为时已晚,但这是我的想法。

    为了在新窗口中打开 SharePoint 网站(响应 HTML),您可以将代码移动到另一个 .aspx 页面,该页面专门用于执行屏幕抓取,仅此而已。返回原始 .aspx 页面,使用 Javascript 在新窗口中打开第二个 .aspx 页面。新窗口将打开,屏幕抓取将处理。

    <a href="javascript:void(0);" onclick="window.open("secondPage.aspx");">Click here to launch SharePoint</a>
    

    如果 SharePoint 链接是相对链接,您需要解析 SharePoint 响应并将所有相对链接替换为绝对链接。这可以通过 HTML Agility Pack 轻松实现。此示例替换了 src 属性,但也可以修改为替换 href 属性:

    Private Function MakeUrlsAbsolute(html As String) As String
        Dim doc As New HtmlDocument()
        Dim url As String
        Dim uri As Uri
    
        doc.LoadHtml(html)
    
        For Each node As HtmlNode In doc.DocumentNode.Descendants.Where(Function(d) d.Attributes.Contains("src")).ToList()
            url = node.GetAttributeValue("src", "")
    
            If Not String.IsNullOrWhiteSpace(url) Then
                uri = New Uri(url, UriKind.RelativeOrAbsolute)
    
                If Not uri.IsAbsoluteUri Then
                    uri = New Uri(New Uri("https://www.yourSharePointSite.com/"), uri)
    
                    node.SetAttributeValue("src", uri.ToString())
                End If
            End If
        Next
    
        Return doc.DocumentNode.OuterHtml
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2017-02-21
      • 2021-07-24
      相关资源
      最近更新 更多