因爲需要批量下載京東的電子發票,隨手用VB .NET寫了個WinForm小工具,爲了圖省事通過 WebBrowser 結合 WebClient 實現了“半自動”的下載,一次要下載很多發票時能省不少力氣。
比較值得記下來的幾個賣點:
1.共享Cookie,實現 WebClient 下載 WebBrowser 中選定的鏈接
''' <summary> ''' 下載保存文件 ''' </summary> ''' <param name="url"></param> Private Shared Sub saveFile(ByVal url As String, doc As HtmlDocument) Dim strCookies As String = doc.Cookie 'If String.IsNullOrEmpty(strCookies) Then ' strCookies = CookieHelper.GetCookieString(url) 'End If Try Using wc As New Net.WebClient wc.Headers.Add("Cookie", strCookies) '共享 WebBrowser.Cookie wc.DownloadFile(url, getFilename(url)) End Using Catch ex As Exception Stop End Try End Sub
2.查找頁面元素,實現自動點擊
Private Shared Function findElement(ByVal doc As HtmlDocument, ByVal elementId As String) As HtmlElement Dim ret As HtmlElement = Nothing Try ret = doc.GetElementById(elementId) Catch ex As Exception Debug.Print("findElement err:{0}", ex.Message) End Try Return ret End Function Private Shared Function findElement(doc As HtmlDocument, ByVal href As String, ByVal innerText As String) As HtmlElement Dim ret As HtmlElement = Nothing For Each element As HtmlElement In doc.Links If element.InnerText = innerText AndAlso element.GetAttribute("href") = href Then ret = element Exit For End If Next Return ret End Function
'通過 InvokeMember 觸發點擊動作
_currElemet = findElement(wb.Document, url, "發票詳情") If _currElemet IsNot Nothing Then _currElemet.InvokeMember("click") Else Debug.Print("findElement NOT FOUND " & url) End If