Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:下载多个文件所需的更好解决方案_Excel_Selenium_Web Scraping_Download_Vba - Fatal编程技术网

Excel VBA:下载多个文件所需的更好解决方案

Excel VBA:下载多个文件所需的更好解决方案,excel,selenium,web-scraping,download,vba,Excel,Selenium,Web Scraping,Download,Vba,我正在使用代码从网站下载csv文件。起初,我尝试了创建InternetExplorer.Application等的传统方法。。这是最慢的方法。后来我发现了selenium包装器的用法,并创建了以下代码: 'Option Explicit Sub ScripHistoryDownloader() Flag5 = 0 Dim selDriver As Object Dim URL As String, Scripcode As String Dim StartDate As String, En

我正在使用代码从网站下载csv文件。起初,我尝试了创建
InternetExplorer.Application
等的传统方法。。这是最慢的方法。后来我发现了selenium包装器的用法,并创建了以下代码:

'Option Explicit

Sub ScripHistoryDownloader()
Flag5 = 0

Dim selDriver As Object
Dim URL As String, Scripcode As String
Dim StartDate As String, EndDate As String
Dim ScripHistPATH As String, DownloadedScripHistFILE As String, ScripHistFILE As String

ScripHistPATH = "R:\DataStore\003__ScripHistory\"

Scripcodez = "500010"

ScripHistFILE = ScripHistPATH & Scripcodez & ".csv"

StartDate = "01/01/1990"
URL = "http://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx?expandable=7&scripcode=" & Scripcodez & "&flag=sp&Submit=G"
ChromeDownloadsURL = "chrome://downloads/"


Set selDriver = CreateObject("SeleniumWrapper.WebDriver")
selDriver.Start "chrome", "http://www.google.com/"
selDriver.Open URL

selDriver.Type "id=ctl00_ContentPlaceHolder1_txtFromDate", StartDate
selDriver.findElementById("ctl00_ContentPlaceHolder1_btnSubmit").Click
selDriver.Click "id=ctl00_ContentPlaceHolder1_btnSubmit"
selDriver.clickAndWait "ctl00_ContentPlaceHolder1_btnDownload"
selDriver.Open ChromeDownloadsURL

'Checking if download is Completed.
downloadChecker:
    Application.Wait (Now + TimeValue("0:00:05"))
    DownloadedSHFileName = selDriver.findElementByClassName("name").Text
    If DownloadedSHFileName = "" Then
    GoTo downloadChecker
    End If

'Finding out where the file is downloaded and moving it to the desired location.
ChromeDownloadsHtml = selDriver.getHtmlSource
PathStartPosition = InStr(ChromeDownloadsHtml, "file:///")
PathStartPosition = PathStartPosition + 8
TempPathText = Mid(ChromeDownloadsHtml, PathStartPosition)
PathEndPosition = InStr(TempPathText, "/" & Scripcodez)
ScrambledPath = Left(TempPathText, PathEndPosition)
DownloadedScripHistFILE = Replace(ScrambledPath, "/", "\") & DownloadedSHFileName
selDriver.stop
MoveOrRenameFile DownloadedScripHistFILE, ScripHistFILE

Set selDriver = Nothing

Flag5 = 1
EndOfBhavCopyDownloader:
End Sub


'Function to Move or rename a file or Folder
Sub MoveOrRenameFile(SourcePath As String, DestinationPath As String)
    Name SourcePath As DestinationPath
End Sub
代码运行完全正常。我每天使用这段代码下载大约3000个文件,每当我的电脑开机时就会被触发。我的问题是,每当触发此代码时,chrome浏览器就会弹出一个cmd窗口。我不希望在使用chrome时出现此弹出窗口。无法像我们使用
InternetExplorer.Application
中的
IE.Visible=False
隐藏IE那样隐藏浏览器和cmd窗口。同时打开浏览器,导航。。。使过程非常缓慢。 是否可以使用
Microsoft.XMLHTTP
对象执行上述代码操作?
我曾经使用过(借助示例)
Microsoft.XMLHTTP
对象,但我还没有在网站上填写表单来生成文件并下载它。(我对使用它不太了解)。。。谁能给我指路吗?感谢您的帮助

注意:发布的代码位于我的项目中的一个模块上:

@pnuts。。谢谢,我会记住的。@Tim Williams谢谢。我认为用那种方法编辑网页表单是不可能的。。或者可能是我没有正确理解。。测试它…不,这是不可能的。。。该文件是由一个函数生成的,我猜。。。使用chrome的Inspect element选项搜索的网页[网页](“&Scripcodez&”&flag=sp&Submit=G)上没有直接指向文件的URL。抱歉-阅读问题太仓促了:我想你被当前方法或类似方法卡住了。是的。。我希望我的项目在完成后在后台运行。。。但是当前的方法不允许我对所有的弹出窗口都这样做,所以我正在寻找一种替代方法。。。