我试图通过网页源代码,将<img src="http://www.dot.com/image.jpg"
添加到HtmlElementCollection
。然后我试图通过foreach循环遍历元素集合中的每个元素并通过url下载图像。用C扫描图像#
这是我到目前为止。我现在的问题是没有什么是下载,我不认为我的元素被标签名称正确添加。如果他们是我似乎无法引用他们的下载。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
string url = urlTextBox.Text;
string sourceCode = WorkerClass.ScreenScrape(url);
StreamWriter sw = new StreamWriter("sourceScraped.html");
sw.Write(sourceCode);
}
private void button2_Click(object sender, EventArgs e)
{
string url = urlTextBox.Text;
WebBrowser browser = new WebBrowser();
browser.Navigate(url);
HtmlElementCollection collection;
List<HtmlElement> imgListString = new List<HtmlElement>();
if (browser != null)
{
if (browser.Document != null)
{
collection = browser.Document.GetElementsByTagName("img");
if (collection != null)
{
foreach (HtmlElement element in collection)
{
WebClient wClient = new WebClient();
string urlDownload = element.FirstChild.GetAttribute("src");
wClient.DownloadFile(urlDownload, urlDownload.Substring(urlDownload.LastIndexOf('/')));
}
}
}
}
}
}
}
你试图去通过网页,并添加了...什么? –
检查urlDownload值以获取有效路径。 – jac