zoukankan      html  css  js  c++  java
  • 随手写了个京东发票助手

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

    源代码发布在:https://github.com/towerbit/JDReceipt

  • 相关阅读:
    【oracle】约束之非空约束
    【oracle】操作表数据之修改和删除
    【oracle】操作表中的数据之添加
    HashMap和HashTable的比较
    java集合类-总纲
    性能测试工具篇(开源&商业)
    比较好的idea工具介绍
    根据mysql某一条记录生成对应实体类的初始化
    Spring+mybatis多数据源切换笔记
    Jmeter的Throughput和平均响应时间计算方法整理
  • 原文地址:https://www.cnblogs.com/towerbit/p/12564821.html
Copyright © 2011-2022 走看看