這是我在空閒時間嘗試做的事情。但是我還不確定我可能面臨的複雜性和問題。我想去像這樣的網址:https://fred.stlouisfed.org/series/DFII5和保存此圖表作爲圖像,本地任何地方在我的電腦上。將html表格/圖表元素轉換爲圖片
我的第一個方法是爲使用的HTML敏捷包:
var document = new HtmlWeb().Load("https://fred.stlouisfed.org/series/DFII5");
var urls = document.DocumentNode.Descendants("img")
.Select(e => e.GetAttributeValue("src", null))
.Where(s => !String.IsNullOrEmpty(s));
,甚至使用的WinForms Web瀏覽器控件:
private void GetWebpage(string url)
{
WebBrowser browser = new WebBrowser();
browser.Navigate(url);
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = (WebBrowser)sender;
var client = new WebClient();
foreach (var img in browser.Document.Images)
{
var image = img as HtmlElement;
var src = image.GetAttribute("src").TrimEnd('/');
if (!Uri.IsWellFormedUriString(src, UriKind.Absolute))
{
src = string.Concat(browser.Document.Url.AbsoluteUri, "/", src);
}
//Append any path to filename as needed
var filename = new string(src.Skip(src.LastIndexOf('/') + 1).ToArray());
File.WriteAllBytes(filename, client.DownloadData(src));
}
}
這兩種方法都能夠從獲取所有圖片該網頁,但圖表是我想要的,它不是一個圖像。
這個任務可能嗎?我需要圖書館/新手來做這件事嗎?我將如何去實現這一目標?注意:在C#中沒有必要這樣做,它可以是Python或其他任何東西。
編輯一些進一步的研究把這些2至我的注意:http://www.princexml.com/和https://wkhtmltopdf.org/
他們都是HTML到PDF庫從我的理解。是否有可能使用這些,只獲得圖表的HTML並將其轉換爲PDF?
使用硒去網頁並截圖。你可以參考https://stackoverflow.com/questions/35921168/how-can-i-get-screenshot-of-specified-element-using-webdriver-in-c-sharp –