Can't download webpage via C# webclient and via request/respond

安稳与你 提交于 2019-12-13 21:28:30

问题


I want to download webpages html code, but have problems with several links. For example: http://www.business-top.info/, http://azerizv.az/ I recieve no html at all using this: 1. WebClient:

using (var client = new WebClient())
            {
                client.Encoding = System.Text.Encoding.UTF8;
                string result = client.DownloadString(resultUrl);
                Console.WriteLine(result);
                Console.ReadLine();
            }

2. Http request/response

var request = (HttpWebRequest)WebRequest.Create(resultUrl);
            request.Method = "POST";
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    StreamReader sr = new StreamReader(stream, Encoding.UTF8);
                    string data = sr.ReadToEnd();
                    Console.WriteLine(data);
                    Console.ReadLine();
                }
            }

There are many such links, so I can't download html manually just via sourse code of web page via browser


回答1:


Some pages load in stages. First they load the core of the page and only then they evaluate any JavaScript inside which loads further content via AJAX. To scrape these pages you will need more advanced content scraping libraries, than just simple HTTP request sender.

EDIT: Here is a question in SO about the same problem that you are having now: Jquery Ajax Web page scraping using c#



来源:https://stackoverflow.com/questions/34989139/cant-download-webpage-via-c-sharp-webclient-and-via-request-respond

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