C# 使用WebClient获取网页HTML
我正在尝试获取“C# 使用WebClient获取网页HTML,c#,web-scraping,C#,Web Scraping,我正在尝试获取“https://codal.ir“, 但结果与GoogleChrome中inspect中的源代码不一致 这是我的密码: private void Button2_Click(object sender, EventArgs e) { using (WebClient client = new WebClient()) // WebClient class inherits IDisposable { string htmlCode = client
https://codal.ir
“,
但结果与GoogleChrome中inspect中的源代码不一致
这是我的密码:
private void Button2_Click(object sender, EventArgs e)
{
using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
{
string htmlCode = client.DownloadString("https://codal.ir");
}
}
假设您只需要静态网页的源代码
private void Button2_Click(object sender, EventArgs e)
{
string urlAddress = "https://codal.ir";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
string data = readStream.ReadToEnd(); // Data variable here
readStream.Close();
}
}
}
您在Chrome中检查的“源代码”很可能是用Javascript修改的。关于抓取单页应用程序之类的问题有很多,如果是这样的话,应该有你想要的答案。要在Chrome(Windows上)中查看页面的源代码,你需要按
Ctrl+u
。这是JS运行和修改页面之前的页面源代码。感谢您的时间,但您的答案仅在html正文中返回如下脚本: