我有这个从谷歌搜索结果(https://www.google.com/search?q=myquery&num=100)提取网址
@"(?<=<h3 class=\""r\""><a href=\""\/url\?q=)(.*?)(?=&)";下面是我从google搜索结果中提取URL的代码
const string regexPattern = @"(?<=<h3 class=\""r\""><a href=\""\/url\?q=)(.*?)(?=&)";
public static string[] TopUrls(string data)
{
Regex regex = new Regex(regexPattern);
MatchCollection collection = regex.Matches(data);
return collection.Cast<Match>()
.Select(m => m.Value)
.ToArray();
}
string downloadUrl = "https://www.google.com" + "/search?q=" + keyword.ToString() + "&num=" + numResults + "&as_qdr=all&ei=LrUVVf7UMrPfsAS7lICgCw&sa=N&biw=1440&bih=690";
fetch.Headers.Set(HttpRequestHeader.Host, "www.google.com");
string data = fetch.DownloadString(downloadUrl);
string[] results = TopUrls(data);从这段代码中,我可以从google搜索结果中提取每个URL。
结果如下: https : //www blogger com/ profile/ 15582992268736301561 https : //www blogger com/ profile /17377873899922361640
如何为这个URL编写regex?http://www.bing.com/search?q=myquery&count=100
(谢谢:)
发布于 2017-01-20 20:19:37
尝试一下像这样的<h2>*?<a\s+[^>]*?href="([^"]*)"
发布于 2017-01-20 19:48:38
为什么不使用必应搜索APIs呢?如果您确实必须解析HTML,则需要查找algo结果。获取带有b_algo类的li标记,并从它们中提取URL。
发布于 2017-01-20 19:53:13
您的第一步是使用:
<cite>(.*?)</cite>然后,您需要另一个正则表达式来删除<strong>标记。
https://stackoverflow.com/questions/41771096
复制相似问题