Html Agility解析ajax响应

时间:2013-11-30 11:33:37

标签: c# regex html-agility-pack

在Ajax Post请求期间,我从Fiddler获取了以下结果,我需要解析它以获取图像链接。是否可以使用HtmlAgility,还是应该使用Regex?

"\r\n\r\n\u003cdiv class=\"media-window\" style=\"position: fixed; left: 25px; top: 25px; right: 25px;\r\n    display: none; bottom: 10px;\"\u003e\r\n    \u003cspan class=\"layer-close\"\u003e\u003ca href=\"javascript:void(0);\" class=\"close\"\u003eClose\u003c/a\u003e\u003c/span\u003e\r\n    \u003cdiv class=\"stop\"\u003e\r\n    \u003c/div\u003e\r\n    \r\n    \u003cdiv class=\"thumbs-wrapper\"\u003e\r\n        \u003cdiv class=\"media-thumbs\"\u003e\r\n            \u003cdiv class=\"media-pager previous-set\"\u003e\r\n                \u003cspan\u003e<< Previous\u003c/span\u003e\u003c/div\u003e\r\n            \u003cul class=\"thumb\"\u003e\r\n                \r\n                \u003cli\u003e\r\n                    \r\n                    \u003ca href=\"javascript:void(0);\" rel=\"http://website/image1.jpg\"\u003e\r\n                        \u003cimg src=\"ttp://website/image1.jpg\" alt=\"0\" /\u003e\r\n                    \u003c/a\u003e\u003c/li\u003e\r\n                \r\n                \u003cli\u003e\r\n                    \r\n                    \u003ca href=\"javascript:void(0);\" rel=\"ttp://website/image1.jpg\"\u003e\r\n                        \u003cimg src=\"ttp://website/image1.jpg\" alt=\"1\" /\u003e\r\n                    \u003c/a\u003e\u003c/li\u003e\r\n                \r\n            \u003c/ul\u003e\r\n            \u003cdiv class=\"media-pager next-set\"\u003e\r\n                \u003cspan\u003eNext >>\u003c/span\u003e\u003c/div\u003e\r\n            \u003cdiv class=\"stop\"\u003e\r\n            \u003c/div\u003e\r\n        \u003c/div\u003e\r\n    \u003c/div\u003e\r\n    \r\n    \u003cdiv class=\"main-image\"\r\n        style=\"overflow: hidden; position: relative;\"\u003e\r\n        \u003cul\u003e\r\n            \u003cli class=\"media-pager previous-img\"\u003e\u003cspan\u003e<< Previous photo\u003c/span\u003e\u003c/li\u003e\r\n            \u003cli class=\"media-pager next-img\"\u003e\u003cspan\u003eNext photo >>\u003c/span\u003e\u003c/li\u003e\r\n        \u003c/ul\u003e\r\n        \u003cdiv class=\"image-wrapper zoom\"\r\n            style=\"position:relative;display:inline-block;\"\r\n            \u003e\r\n            \r\n            \r\n            \u003cscript type=\"text/javascript\"\u003e\r\n                $(document).ready(function () {\r\n                    $(\"ul.thumb li\").each(function () {\r\n                        if ($(this).children(\"a\").children(\"img\").attr(\"alt\") == \u00270\u0027) {\r\n"

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

string str = "\r\n\r\n\u003cdiv class=\"media-window\" style=\"position: fixed; left: 25px; top: 25px; right: 25px;\r\n    display: none; bottom: 10px;\"\u003e\r\n    \u003cspan class=\"layer-close\"\u003e\u003ca href=\"javascript:void(0);\" class=\"close\"\u003eClose\u003c/a\u003e\u003c/span\u003e\r\n    \u003cdiv class=\"stop\"\u003e\r\n    \u003c/div\u003e\r\n    \r\n    \u003cdiv class=\"thumbs-wrapper\"\u003e\r\n        \u003cdiv class=\"media-thumbs\"\u003e\r\n            \u003cdiv class=\"media-pager previous-set\"\u003e\r\n                \u003cspan\u003e<< Previous\u003c/span\u003e\u003c/div\u003e\r\n            \u003cul class=\"thumb\"\u003e\r\n                \r\n                \u003cli\u003e\r\n                    \r\n                    \u003ca href=\"javascript:void(0);\" rel=\"http://website/image1.jpg\"\u003e\r\n                        \u003cimg src=\"ttp://website/image1.jpg\" alt=\"0\" /\u003e\r\n                    \u003c/a\u003e\u003c/li\u003e\r\n                \r\n                \u003cli\u003e\r\n                    \r\n                    \u003ca href=\"javascript:void(0);\" rel=\"ttp://website/image1.jpg\"\u003e\r\n                        \u003cimg src=\"ttp://website/image1.jpg\" alt=\"1\" /\u003e\r\n                    \u003c/a\u003e\u003c/li\u003e\r\n                \r\n            \u003c/ul\u003e\r\n            \u003cdiv class=\"media-pager next-set\"\u003e\r\n                \u003cspan\u003eNext >>\u003c/span\u003e\u003c/div\u003e\r\n            \u003cdiv class=\"stop\"\u003e\r\n            \u003c/div\u003e\r\n        \u003c/div\u003e\r\n    \u003c/div\u003e\r\n    \r\n    \u003cdiv class=\"main-image\"\r\n        style=\"overflow: hidden; position: relative;\"\u003e\r\n        \u003cul\u003e\r\n            \u003cli class=\"media-pager previous-img\"\u003e\u003cspan\u003e<< Previous photo\u003c/span\u003e\u003c/li\u003e\r\n            \u003cli class=\"media-pager next-img\"\u003e\u003cspan\u003eNext photo >>\u003c/span\u003e\u003c/li\u003e\r\n        \u003c/ul\u003e\r\n        \u003cdiv class=\"image-wrapper zoom\"\r\n            style=\"position:relative;display:inline-block;\"\r\n            \u003e\r\n            \r\n            \r\n            \u003cscript type=\"text/javascript\"\u003e\r\n                $(document).ready(function () {\r\n                    $(\"ul.thumb li\").each(function () {\r\n                        if ($(this).children(\"a\").children(\"img\").attr(\"alt\") == \u00270\u0027) {\r\n";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(str);
foreach (var selectNode in doc.DocumentNode.SelectNodes("//img[@src]"))
{
    Console.WriteLine(selectNode.Attributes["src"].Value);
}

更新: 我尝试从google.com解析图片的代码:

var url = "http://google.com";
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);

HtmlDocument doc = new HtmlDocument();
doc.Load(await response.Content.ReadAsStreamAsync());

foreach (var selectNode in doc.DocumentNode.SelectNodes("//img[@src]"))
{
      Console.WriteLine(selectNode.Attributes["src"].Value);
}

答案 1 :(得分:0)

答案很简单:

var decodedString = Regex.Unescape(yourString);

谢谢大家。

答案 2 :(得分:0)

是的,它正在发挥作用:

var decodedString = Regex.Unescape(yourString);