我試圖通過網頁源代碼,將<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