怎么把网站上的图片取出来,知道其在网页中的位置
取出来放在电脑里,然后给后面的程序调用,C#语言的,用什么方式 --------------------编程问答-------------------- 请教大神们 --------------------编程问答-------------------- 先将网页全部内容取出来
//取网页内容:方法1
WebBrowser webBrowser = new WebBrowser();
webBrowser.Navigate("网址");
string htmlContex = webBrowser.DocumentText;
方法2:参考
--------------------编程问答-------------------- 然后将它保存到电脑里呢?? --------------------编程问答-------------------- private void button1_Click(object sender, EventArgs e)
public static string GetImgUrl(string htmlContent)
{
string imageUrl = string.Empty;
//string sPattern = @"^<img/s+[^>]*>";
Regex regex = new Regex(@"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>",
RegexOptions.Compiled);
Match match = regex.Match(htmlContent.ToLower());
if (match.Success)
imageUrl = match.Result("${url}");
return imageUrl;
}
{
string httpSourse;
string regexImgPattern = @"<img[^>]+(data-ks-lazyload)\s*=\s*""?([^"">]+)""?(?:[^>]+([^"">]+)""?)?";
myRegex = new Regex(regexImgPattern, RegexOptions.IgnoreCase);
myMatch = myRegex.Match(httpSourse);
label7.Text = "";
while (myMatch.Success == true)
{
listBoxImage.Items.Add(myMatch.Groups[2].Value);
myMatch = myMatch.NextMatch();
}
}
private void listBoxImage_Click(object sender, EventArgs e)
{
string s = textBoxUrl.Text;
int index = s.LastIndexOf("/");
if (index == -1)
{
index = s.LastIndexOf("\\");
}
string urlpath = s.Substring(0, index + 1);
string path = listBoxImage.SelectedItem.ToString();
string path1 = path;
if (path.StartsWith("http://") == false)
{
path1 = urlpath + path;
}
string imageUrl = path1;
try
{
pictureBox1.Image = Image.FromStream(client.OpenRead(imageUrl));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" + path1);
}
} --------------------编程问答-------------------- 根据Url下载文件
--------------------编程问答-------------------- 要是有很多图片呢
void myTest()
{
Stream reader = null;
FileStream stream = null;
try
{
//文件地址
WebRequest request = WebRequest.Create(@"http://pic.nipic.com/2007-12-10/20071210224154962_2.jpg");
WebResponse response = request.GetResponse();
reader = response.GetResponseStream();
stream = File.Open("D:myPic.png", FileMode.OpenOrCreate, FileAccess.Write);
byte[] buf = new byte[512];
int cnt;
while ((cnt = reader.Read(buf, 0, buf.Length)) > 0)
{
stream.Write(buf, 0, cnt);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (reader != null)
{
reader.Close();
stream.Close();
}
}
}
补充:.NET技术 , C#