【问题标题】:Book list - getting book details from amazon using Excel VBA barcode lookups图书清单 - 使用 Excel VBA 条形码查找从亚马逊获取图书详细信息
【发布时间】:2011-04-23 15:43:56
【问题描述】:

我有一个条形码阅读器和一堆书。对于每本书,我想在 Excel 电子表格中列出书名和作者。

我的观点是,一些连接到亚马逊网络服务的 VBA 代码会使这更容易。

我的问题是 - 以前没有人这样做过吗?你能指出我最好的例子吗?

【问题讨论】:

    标签: vba excel amazon-web-services barcode


    【解决方案1】:

    我认为这是一个简单的谷歌搜索,但结果比我预期的要困难。

    事实上,我找不到一个基于 VBA ISBN 的程序来从网络上获取图书数据,所以决定做一个。

    这是一个使用来自xisbn.worldcat.org 的服务的 VBA 宏。示例here.。这些服务是免费的,不需要身份验证。

    为了能够运行它,您应该在工具-> 参考(在 VBE 窗口中)检查“Microsoft xml 6.0”库。

    此宏从当前单元格中获取 ISBN(10 位数字),并用作者和标题填充以下两列。您应该能够轻松地遍历整列。

    代码已经过测试(嗯,有点),但那里没有错误检查。

     Sub xmlbook()
     Dim xmlDoc As DOMDocument60
     Dim xWords As IXMLDOMNode
     Dim xType As IXMLDOMNode
     Dim xword As IXMLDOMNodeList
     Dim xWordChild As IXMLDOMNode
     Dim oAttributes As IXMLDOMNamedNodeMap
     Dim oTitle As IXMLDOMNode
     Dim oAuthor As IXMLDOMNode
     Set xmlDoc = New DOMDocument60
     Set xWords = New DOMDocument60
     xmlDoc.async = False
     xmlDoc.validateOnParse = False
     r = CStr(ActiveCell.Value)
    
     xmlDoc.Load ("http://xisbn.worldcat.org/webservices/xid/isbn/" _
                  + r + "?method=getMetadata&format=xml&fl=author,title")
    
     Set xWords = xmlDoc
    
         For Each xType In xWords.ChildNodes
             Set xword = xType.ChildNodes
             For Each xWordChild In xword
                 Set oAttributes = xWordChild.Attributes
                 On Error Resume Next
                 Set oTitle = oAttributes.getNamedItem("title")
                 Set oAuthor = oAttributes.getNamedItem("author")
                 On Error GoTo 0
             Next xWordChild
         Next xType
      ActiveCell.Offset(0, 1).Value = oTitle.Text
      ActiveCell.Offset(0, 2).Value = oAuthor.Text
     End Sub
    

    我没有通过亚马逊,因为他们新的“直截了​​当”的身份验证协议......

    【讨论】:

    • 正是我想要的! PowerPivot 非刷新功能被阻止! ;)
    【解决方案2】:

    这对我很有帮助!

    我已经更新了宏,允许它循环通过一列 ISBN 编号,直到它到达一个空单元格。

    它还搜索出版商、年份和版本。

    如果某些字段不可用,我添加了一些基本的错误检查。

    Sub ISBN()
     Do
     Dim xmlDoc As DOMDocument60
     Dim xWords As IXMLDOMNode
     Dim xType As IXMLDOMNode
     Dim xword As IXMLDOMNodeList
     Dim xWordChild As IXMLDOMNode
     Dim oAttributes As IXMLDOMNamedNodeMap
     Dim oTitle As IXMLDOMNode
     Dim oAuthor As IXMLDOMNode
     Set xmlDoc = New DOMDocument60
     Set xWords = New DOMDocument60
     xmlDoc.async = False
     xmlDoc.validateOnParse = False
     r = CStr(ActiveCell.Value)
    
     xmlDoc.Load ("http://xisbn.worldcat.org/webservices/xid/isbn/" _
                  + r + "?method=getMetadata&format=xml&fl=author,title,year,publisher,ed")
    
     Set xWords = xmlDoc
    
         For Each xType In xWords.ChildNodes
             Set xword = xType.ChildNodes
             For Each xWordChild In xword
                 Set oAttributes = xWordChild.Attributes
                 On Error Resume Next
                 Set oTitle = oAttributes.getNamedItem("title")
                 Set oAuthor = oAttributes.getNamedItem("author")
                 Set oPublisher = oAttributes.getNamedItem("publisher")
                 Set oEd = oAttributes.getNamedItem("ed")
                 Set oYear = oAttributes.getNamedItem("year")
                 On Error GoTo 0
             Next xWordChild
         Next xType
      On Error Resume Next
      ActiveCell.Offset(0, 1).Value = oTitle.Text
    
      On Error Resume Next
      ActiveCell.Offset(0, 2).Value = oAuthor.Text
    
      On Error Resume Next
      ActiveCell.Offset(0, 3).Value = oPublisher.Text
    
      On Error Resume Next
      ActiveCell.Offset(0, 4).Value = oYear.Text
    
      On Error Resume Next
      ActiveCell.Offset(0, 5).Value = oEd.Text
    
    
      ActiveCell.Offset(1, 0).Select
      Loop Until IsEmpty(ActiveCell.Value)
    
     End Sub
    

    【讨论】:

      【解决方案3】:

      当我试图做同样的事情时,我刚刚发现了这个帖子。不幸的是,我在 MAC 上,所以这些答案无济于事。通过一些研究,我能够通过这个模块让它在 MAC Excel 中工作:

      Option Explicit
      
      ' execShell() function courtesy of Robert Knight via StackOverflow
      ' http://stackoverflow.com/questions/6136798/vba-shell-function-in-office-    2011-for-mac
      
      Private Declare Function popen Lib "libc.dylib" (ByVal command As String,       ByVal mode As String) As Long
      Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
      Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
      Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
      
      Function execShell(command As String, Optional ByRef exitCode As Long) As String
          Dim file As Long
          file = popen(command, "r")
      
          If file = 0 Then
              Exit Function
          End If
      
          While feof(file) = 0
              Dim chunk As String
              Dim read As Long
              chunk = Space(50)
              read = fread(chunk, 1, Len(chunk) - 1, file)
              If read > 0 Then
                  chunk = Left$(chunk, read)
                  execShell = execShell & chunk
              End If
          Wend
      
          exitCode = pclose(file)
      End Function
      
      Function HTTPGet(sUrl As String) As String
      
          Dim sCmd As String
          Dim sResult As String
          Dim lExitCode As Long
          Dim sQuery As String
      
          sQuery = "method=getMetadata&format=xml&fl=*"
          sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
          sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
      
          sResult = execShell(sCmd, lExitCode)
      
          ' ToDo check lExitCode
      
          HTTPGet = sResult
      
      End Function
      
      Function getISBNData(isbn As String) As String
        Dim sUrl As String
        sUrl = "http://xisbn.worldcat.org/webservices/xid/isbn/" & isbn
        getISBNData = HTTPGet(sUrl)
      
      End Function
      
      
      
      Function getAttributeForISBN(isbn As String, info As String) As String
        Dim data As String
        Dim start As Integer
        Dim finish As Integer
      
      
       data = getISBNData(isbn)
       start = InStr(data, info) + Len(info) + 2
       finish = InStr(start, data, """")
       getAttributeForISBN = Mid(data, start, finish - start)
      
      
      End Function
      

      这不是我的全部原创作品,我从另一个网站将它们粘贴在一起,然后自己完成了。现在您可以执行以下操作:

      getAttributeForISBN("1568812019","title")

      这将返回该书的标题。当然,您可以将此公式应用于 A 列中的所有 ISBN,以查找多个标题、作者或其他任何内容。

      希望这对其他人有帮助!

      【讨论】:

        【解决方案4】:

        如果条形码是 ISBN,这似乎很可能,也许您可​​以使用:amazon.com/Advanced-Search-Books/b?ie=UTF8&node=241582011

        【讨论】:

        • 我猜OP的问题是如何将书名放入单元格
        • 使用 Excel 从网页抓取信息经常被发布,因此搜索起来应该不难。
        • 没有发现任何对在 VBA 中解析 HTML 真正有用的东西。我用 XML 写了一个答案。您是否介意在您的答案中分享一个用于在 VBA 中解析 HTML 的好指针(而不是仅处理表格的 .qry 解决方案!)? Tnx!
        • 供将来参考这里stackoverflow.com/questions/4061656/…是答案中的一个很好的建议
        猜你喜欢
        • 1970-01-01
        • 2012-08-05
        • 2013-01-18
        • 1970-01-01
        • 2012-07-06
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 2011-03-24
        相关资源
        最近更新 更多