因此,我尝试使用c#
和google
进行窃检查,经过几次调试后,我的代码不再起作用。我尝试查看该异常,并发现它是429 Too Many Requests error
。我想要做的是要么绕过此错误(因为我仍然可以从同一台PC访问google),要么花点时间再试一次。我该怎么办?
搜索代码:
private void SearchAndWrite(string text)
{
string txtKeyWords = text;
listBox1.Items.Clear();
StringBuilder sb = new StringBuilder();
byte[] ResultsBuffer = new byte[8192];
string SearchResults = "http://google.com/search?q=" + txtKeyWords.Trim();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SearchResults);
//request.Headers["X-My-Custom-Header"] = "'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)\\AppleWebKit / 537.36(KHTML, like Gecko) Cafari / 537.36'";
try
{
int count = 0;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
count = resStream.Read(ResultsBuffer, 0, ResultsBuffer.Length);
string tempString = null;
do
{
if (count != 0)
{
tempString = Encoding.ASCII.GetString(ResultsBuffer, 0, count);
sb.Append(tempString);
}
}
while (count > 0);
string sbb = sb.ToString();
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.OptionOutputAsXml = true;
html.LoadHtml(sbb);
HtmlNode doc = html.DocumentNode;
foreach (HtmlNode link in doc.SelectNodes("//a[@href]"))
{
//HtmlAttribute att = link.Attributes["href"];
string hrefValue = link.GetAttributeValue("href", string.Empty);
if (!hrefValue.ToString().ToUpper().Contains("GOOGLE") && hrefValue.ToString().Contains("/url?q=") && hrefValue.ToString().ToUpper().Contains("HTTP://"))
{
int index = hrefValue.IndexOf("&");
if (index > 0)
{
hrefValue = hrefValue.Substring(0, index);
listBox1.Items.Add(hrefValue.Replace("/url?q=", ""));
}
}
}
}
catch(Exception e)
{
MessageBox.Show(e.ToString(), "An error has occurred!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
答案 0 :(得分:1)
虽然我看不到所有代码,也看不到任何递归,但我假设您要多次调用SearchAndWrite,但您可能需要对查询进行速率限制。
尝试在每个请求之间放置5或10秒的等待时间,如果问题消失了,那么您需要找到一种避免对Google进行查询的方法。
考虑使用队列和工作程序循环。