从谷歌下载洞察CSV?(download CSV from google Insight?)

2019-10-17 12:59发布

我成功地做到了前在过去的4-5也许6个月回来,但现在我看到的网站已经改变了。我是能够得到使用的HttpWebRequest所需的搜索结果的问题是与下载的CSV文件。

下载不会工作我使用Web客户端得到的所有Cookie,但它仍然不会工作复制完全一样的。

当我这样做,我得到这个文件中

..... META HTTP-当量= “刷新” 内容= “0; URL = 'HTTP://www.google.com/trends#content=1&geo=US-AL&q=snooker&cmpt=q&hl=en-AU'” > location.replace( “http://www.google.com/trends#content\x3d1\x26geo\x3dUS-AL\x26q\x3dsnooker\x26cmpt\x3dq\x26hl\x3den-AU”)

下载文件中的代码如下:

public void downloadsheet(string url, string path)
    {
        try
        {
            using (WebClient client = new WebClient())
            {



                string tmpCookieString = string.Empty;

                string[] array = webBrowser1.Document.Cookie.Split(new char[]
                        {
                            ';'
                        });
                for (int i = 0; i < array.Length; i++)
                {
                    string cookie = array[i];
                    string name = cookie.Split(new char[]
                            {
                                '='
                            })[0];
                    string value = cookie.Substring(name.Length + 1);

                    //client.Headers.Add(name, value);
                    if (i < array.Length - 1)
                    {
                        tmpCookieString = tmpCookieString + name + "=" + value + ";";
                    }
                    else
                    {
                        tmpCookieString = tmpCookieString + name + "=" + value;
                    }
                }

                client.Headers.Add(HttpRequestHeader.Cookie, tmpCookieString);
                client.Headers.Add("Accept", "text/html, application/xhtml+xml, */*");
                client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)");
                client.Headers.Add("Accept-Language", "en-US");
                using (FileStream file = File.Create(path))
                {
                    byte[] bytes = client.DownloadData(url);
                    file.Write(bytes, 0, bytes.Length);
                }
            }
        }
        catch (Exception exp_DE)
        {
        }
    }

URL是使用方法是:

http://www.google.com/trends/trendsReport?hl=en-AU&q=snooker&geo=US-AL&cmpt=q&content=1&export=2

任何帮助非常赞赏。

更多信息:

如果我使用WebBrowser控件导航到相应的链接上面它打开了一个对话框..

Answer 1:

问题是的HttpOnly饼干(即SIDHSID)从丢失WebBrowser.Document.Cookie用于安全目的 。

这里是解决方案:

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);
const int INTERNET_COOKIE_HTTPONLY = 0x00002000;

private static string GetGlobalCookies(string uri)
{
    uint datasize = 2048;
    StringBuilder cookieData = new StringBuilder((int)datasize);
    if (InternetGetCookieEx(uri, null, cookieData, ref datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)
        && cookieData.Length > 0)
    {
        return cookieData.ToString();
    }
    else
    {
        return null;
    }
}

public void downloadsheet(string url, string path)
{
    try
    {
        using (WebClient client = new WebClient())
        {
            string tmpCookieString = GetGlobalCookies(webBrowser1.Url.AbsoluteUri);

            client.Headers.Add(HttpRequestHeader.Cookie, tmpCookieString);

            client.Headers.Add("Accept", "text/html, application/xhtml+xml, */*");
            client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)");
            client.Headers.Add("Accept-Language", "en-US");
            using (FileStream file = File.Create(path))
            {
                byte[] bytes = client.DownloadData(url);
                file.Write(bytes, 0, bytes.Length);
            }
        }
    }
    catch (Exception exp_DE)
    {
    }
}

当然,你应该在你的帐户调用InternetGetCookieEx之前签署。



文章来源: download CSV from google Insight?