我正在写一个小的C#应用程序来管理我们的供应商的安全数据表(化学品)。
目前,我手动搜索化学品,保存pdf,并在我的程序中添加一个指向pdf的链接。问题是我仍然有很多化学物质要去,所以最好是自动化这个过程。
例如:一种化学品具有以下部件号: 271004
包含pdf的链接如下:
Link
我一直在阅读页面源代码,但找不到pdf的链接。
但目前我对html/javascript的了解有限……
有没有办法从网站上提取pdf文件?
提前感谢您的任何建议:)
发布于 2014-10-07 15:31:53
在页面中查找id为"msdsPageFrame“的iframe元素。该元素的src属性包含指向您的PDF的url。下载该url。
如果您有关于如何下载URL或如何在搜索id时解析页面的问题,请询问另一个问题。
发布于 2014-10-07 17:20:01
现在,我可以使用产品代码直接访问pdf文件:
www.sigmaaldrich.com/MSDS/MSDS/DisplayMSDSPage.do?country=NL&language=EN-generic&productNumber=271004&brand=SIAL&PageToGoToURL=null
使用以下代码,我尝试下载pdf:
private void Download()
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); // Uses the Event Handler to check whether the download is complete
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); // Uses the Event Handler to check for progress made
webClient.DownloadFileAsync(new Uri("http://www.sigmaaldrich.com/MSDS/MSDS/DisplayMSDSPage.do?country=NL&language=EN-generic&productNumber=271004&brand=SIAL&PageToGoToURL=null"), @"C:\Users\test\Downloads\newfile.pdf"); // Defines the URL and destination directory for the downloaded file
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Debug.WriteLine("DownloadProgressChangedEventHandler");
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
Debug.WriteLine("AsyncCompletedEventHandler");
}然而,这并不起作用。问题是首先生成pdf (需要几秒钟)。但是,AsyncCompletedEventHandler会立即触发。我认为这就是为什么pdf文件没有下载的问题。
https://stackoverflow.com/questions/26230485
复制相似问题