我有以下问题:
我编写了一个程序,它使用谷歌图像搜索来提取指向 jpg 文件的链接。但是在链接前面我有一个 15 字符长的字符串,我无法删除。
public const int resolution = 1920;
public const int DEFAULTIMGCOUNT = 40;
public void getimages(string searchpatt)
{
string blub = "http://images.google.com/images?q=" + searchpatt + "&biw=" + resolution;
WebClient client = new WebClient();
string html = client.DownloadString(blub); //Downloading the gooogle page;
MatchCollection mc = Regex.Matches(html,
@"(https?:)?//?[^'<>]+?\.(jpg|jpeg|gif|png)");
int mccount = 0; // Keep track of imgurls
string[] results = new string[DEFAULTIMGCOUNT]; // String Array to place the Urls
foreach (Match m in mc) //put matches in string array
{
results[mccount] = m.Value;
mccount++;
}
string remove = "/imgres?imgurl=";
char[] removetochar = remove.ToCharArray();
foreach (string s in results)
{
if (s != null)
{
s.Remove(0, 15);
Console.WriteLine(s+"\n");
}
else { }
}
// Console.Write(html);
}
我尝试了删除和修剪启动,但它们都没有工作,我无法弄清楚我的失败。
我像这样解决了
for (int i = 0; i < results.Count(); i++)
{
if (results[i] != null)
{
results[i] = results[i].Substring(15);
Console.Write(results[i]+"\n");
}
}