简体   繁体   English

无法通过URL获取正确的网页源代码-C#

[英]Can't get the right webpage source code by URL - c#

I'm trying to get the source code of google's Search by Image page. 我正在尝试获取Google的“按图像搜索”页面的源代码。 So, the Search by Image URL is https://www.images.google.com/searchbyimage?image_url=x x is the image URL. 因此,“按图像URL搜索”是https://www.images.google.com/searchbyimage?image_url=x x是图像URL。

I use that code, but the source code I get is the source code of google images, not the source code of the specific search result webpage. 我使用该代码,但是我得到的源代码是google images的源代码,而不是特定搜索结果网页的源代码。

public static string GetSourceCode(string url)
{
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
    myRequest.Method = "GET";
    WebResponse myResponse = myRequest.GetResponse();
    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
    string result = sr.ReadToEnd();
    sr.Close();
    myResponse.Close();

    return result;
}

When you execute a Google Search, Google only returns a bit of HTML (which you see, when you open your result-string in a browser). 当您执行Google搜索时,Google只返回一点HTML(在浏览器中打开结果字符串时会看到)。 The JavaScript, in your result, is run to load the actual search results. 结果中的JavaScript将运行以加载实际的搜索结果。

As a side note, in Chrome (at least, that's where I have actually tested it) you can watch the JavaScript run, if you set a breakpoint at 'load'. 附带说明一下,如果在“加载”中设置了断点,则在Chrome(至少是我实际测试过的地方)中,可以观看JavaScript的运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM