【问题标题】:WinRT web page parse / DocumentNode.InnerHtml = "URI" rather than page htmlWinRT 网页解析 / DocumentNode.InnerHtml = "URI" 而不是页面 html
【发布时间】:2012-11-28 18:05:53
【问题描述】:

我正在尝试为我的大学创建一个包含课程表的 Metro 应用程序。我使用 HAP+Fizzler 解析页面并获取数据。

计划链接给我@Too many automatic redirections@ 错误。 我发现CookieContainer可以帮助我,但不知道如何实现它。

        CookieContainer cc = new CookieContainer();
        request.CookieContainer = cc;

我的代码:

            public static HttpWebRequest request;
    public string Url = "http://cist.kture.kharkov.ua/ias/app/tt/f?p=778:201:9421608126858:::201:P201_FIRST_DATE,P201_LAST_DATE,P201_GROUP,P201_POTOK:01.09.2012,31.01.2013,2423447,0:";
    public SampleDataSource()
    {

        HtmlDocument html = new HtmlDocument();
        request = (HttpWebRequest)WebRequest.Create(Url);
        request.Proxy = null;
        request.UseDefaultCredentials = true;
        CookieContainer cc = new CookieContainer();
        request.CookieContainer = cc;
        html.LoadHtml(request.RequestUri.ToString());
        var page = html.DocumentNode;

String ITEM_CONTENT = null;
foreach (var item in page.QuerySelectorAll(".MainTT")) 
{
    ITEM_CONTENT = item.InnerHtml;
}
      }

使用 CookieContainer 我没有收到错误,但 DocumentNode.InnerHtml 出于某种原因获取了我的 URI 的值,而不是页面 html。

【问题讨论】:

    标签: c# .net microsoft-metro html-agility-pack winrt-xaml


    【解决方案1】:

    你只需要改变一行。

    替换

     html.LoadHtml(request.RequestUri.ToString());
    

     html.LoadHtml(new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd());
    

    编辑

    首先将您的方法标记为async

    request.CookieContainer = cc;
    var resp = await request.GetResponseAsync();
    html.LoadHtml(new StreamReader(resp.GetResponseStream()).ReadToEnd());
    

    【讨论】:

    • 检查一下,也许你有一些 GetResponseAsync 等。我现在无法在 Metro 应用程序中测试它。我在Win7中测试了上面的代码并且可以工作。这里的关键是您必须获取响应流并对其进行处理。
    • 我处理它。但是 GetResponseAsync 没有 GetResponseStream() 或类似的东西。
    • @MaksMartynov Async 方法返回 Tasks。我猜你看到的是它的方法。查看编辑。
    【解决方案2】:

    如果您想下载网页代码,请尝试使用此方法(使用HttpClient):

    public async Task<string> DownloadHtmlCode(string url)
        {
            HttpClientHandler handler = new HttpClientHandler { UseDefaultCredentials = true, AllowAutoRedirect = true };
            HttpClient client = new HttpClient(handler);
            HttpResponseMessage response = await client.GetAsync(url);                  
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            return responseBody;
        }
    

    【讨论】:

      【解决方案3】:

      如果您想解析下载的 htmlcode,您可以使用 Regex 或 LINQ。我有一些使用 LINQ 解析 html 代码的示例,但在您应该使用 HtmlAgilityPack 库将代码加载到 HtmlDocument 之前。然后你可以通过这种方式加载:html.LoadHtml(temphtml); 当你这样做时,你可以解析你的 HtmlDocument:

      //This is for img links parse-example:
      IEnumerable<HtmlNode> imghrefNodes = html.DocumentNode.Descendants().Where(n => n.Name == "img");
      foreach (HtmlNode img in imghrefNodes)
      {
         HtmlAttribute att = img.Attributes["src"];
         //in att.Value you can find your img url
         //Here you can do everything what you want with all img links by editing att.Value
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-06
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        • 2011-06-13
        • 2020-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多