因为需要批量下载京东的电子发票,随手用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