【发布时间】:2016-02-14 01:42:50
【问题描述】:
我正在开发一个链接提取器 c# 应用程序。根据关键字抓取网址。就像当用户使用 txtKeyWords 文本框字段搜索关键字并点击搜索按钮时,他将能够使用开放的谷歌搜索运算符 "http://www.google.com/search?num=1000&q=" 在任何关键字上获取所需的网址。
现在我想知道如何添加此代码 http://www.google.com/search?num=50&q=allinurl:site:.edu 以便仅获取 rb1.Checked 下的 edu 链接。就像当用户检查无线电 btn1 并在 txtKeyWords 文本框字段中输入所需的关键字时,他能够获取与查询相关的 .edu 链接。我已经做了几次尝试,但无法这样做。这是Search Button的代码
listBox1.Items.Clear();
StringBuilder sb = new StringBuilder();
byte[] ResultsBuffer = new byte[8192];
string SearchResults = "http://www.google.com/search?num=1000&q=" + txtKeyWords.Text.Trim();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SearchResults);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
count = resStream.Read(ResultsBuffer, 0, ResultsBuffer.Length);
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=", ""));
}
}
}
【问题讨论】:
标签: c# .net wpf radio-button