【发布时间】:2019-11-15 04:06:46
【问题描述】:
在 C# 中解析 Google SERP - 我认为正则表达式是问题所在。你能帮助我吗? 它总是返回位置 0。
public static int GetPosition(Uri url, string searchTerm)
{
string text = string.Format("http://www.google.com/search?num=1000&q={0}&btnG=Search", HttpUtility.UrlEncode(searchTerm));
Console.WriteLine(text);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(text);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
string html = reader.ReadToEnd();
return FindPosition(html, url);
}
}
}
private static int FindPosition(string html, Uri url)
{
string lookup = "(<h3 class=\"r\"><a href=\"/url\\?q=)(\\w+[a-zA-Z0-9.\\-?=/:]*)";
[...]
}
}
}
【问题讨论】: