【问题标题】:WebClient HeadersWebClient 标头
【发布时间】:2020-05-02 10:11:46
【问题描述】:

我正在使用 WebClient 来抓取 google 搜索。 我一直收到“无法访问此页面” 直到我更改了 User-Agent 标头:

            string page = string.Format("https://www.google.com/search?q={0}&hl=en", my_stocks[order].Symbole+" stock");
            WebClient client = new WebClient ();
            client.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
            string r = client.DownloadString(page);

但是当我在我的 chrome 中搜索相同的东西时,html 呈现的不同。 所以当我使用带有https://www.whatismybrowser.com/detect/what-is-my-user-agent的chrome时,我尝试将标题更改为相同的 但再次获得“无法访问此页面”。 我在这里错过了什么?

【问题讨论】:

    标签: c# .net webclient


    【解决方案1】:

    我的 2 美分 ...

    由于单页应用程序的涌入,网页抓取已不再是过去的样子,因为页面通常不再在服务器端呈现。

    Google 搜索很可能是使用异步 REST 查询而不是服务器端呈现的页面提供的。

    当您进行 Google 搜索时,在 Chrome 选项卡中查看网络跟踪,您可能会看到许多不同的网络请求。

    我建议您寻找更具体的 API 来处理您希望发出的请求类型。

    【讨论】:

      猜你喜欢
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2012-07-04
      • 2010-10-28
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多