无法从网址获取html源代码

时间:2014-10-23 13:16:26

标签: html webrequest getresponse

我已经用我的问题阅读了大部分答案。但我从这个网址中得不到任何结果。好吧,实际上我得到了" "

这是网址:

http://www.casadellibro.com/busqueda-generica?busqueda=9783126759120&nivel=5&auto=0&maxresultados=-1

我的代码:

System.Net.WebRequest req = System.Net.WebRequest.Create(url);
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string response = sr.ReadToEnd();

有什么想法吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

从网站获取HTML代码。你可以使用这样的代码。

string urlAddress = "http://google.com";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
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();
  response.Close();
  readStream.Close();
}

这将为您提供网站上返回的HTML代码。但是通过LINQ查找文本并不容易。也许最好使用正则表达式,但这与HTML代码不兼容

阅读所有反应:Get HTML code from website in C#