问题
I want to scrape google suggest in german language:
string url = "http://google.de/complete/search?output=toolbar&q=" + txtResults.Lines[i].Replace(" ", "%20%");
WebClient client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0");
client.Headers.Add("Accept-Encoding:", "gzip, deflate");
client.Headers.Add("Connection:", "keep-alive");
doc.Load(client.OpenRead(url), System.Text.Encoding.UTF8, true);
But i always get international results whereas in the browser i get the german results by the given .de url. So i added the header:
client.Headers.Add("Accept-Language:", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
But now i get the error: Specified value has invalid HTTP Header characters
I also tried to set the language of my application by
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
but this doens't help either.
回答1:
Try removing the colon (:) from your header name:
client.Headers.Add("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
来源:https://stackoverflow.com/questions/13760988/webclient-and-language