【问题标题】:Script to read from Excel and interact with website and write down the result again to Excel从 Excel 读取并与网站交互并将结果再次写入 Excel 的脚本
【发布时间】:2018-01-15 05:25:59
【问题描述】:

我正在尝试自动化一些重复性工作以从网站获取结果, 此链接Drugs.com,检查两种药物之间的相互作用。
我必须从 Excel 表中取出两种药物的文本,然后在网站上输入它们以检查它们之间的相互作用。 这是我的 Excel 工作表示例:

column(A)         Column(B)
(A1)candesartan       benazepril
(A2)eprosartan        captopril
(A3)irbesartan        enalapril

当我按下“检查交互”时,必须提取下一页的结果并返回其中一个树交互:

-专业
-中等
-次要

这必须将结果写入列(c)

我是 autoit 的初学者,但我可以编写一些脚本,尽管有很多错误。 如果有人可以纠正/帮助我解决我的代码中的错误,我将不胜感激。如果有人可以帮助我使用正确的关键字,我也将不胜感激,以便我可以谷歌搜索示例和解决方案。 谢谢大家。

  #include <Excel.au3>
#include <IE.au3>
#include <File.au3>

Local $sWorkbook = "C:\Users\Aligaaly\Desktop\autoit\test\drugs.xlsx"
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook)
$oWorkbook = _Excel_BookAttach($sWorkbook)


GLOBAL $oIE = _IECreate("https://www.drugs.com/drug_interactions.php")
Local $oInputs = _IETagNameGetCollection($oIE, "input")

For $oInput In $oInputs
      $text_form1 = _IEGetObjById($oIE, "livesearch-interaction")
      If StringLower($oInput.classname) == "input-button search-button" and _IEFormElementGetValue($oInput)  Then ; it is an input
                  Global $oInput_btn = $oInput
      EndIf
Next


WinActivate("[CLASS:XLMAIN]", "")

For $i = 1 To 5
Global $sResulta = _Excel_RangeRead($oWorkbook, Default, 'A' & $i & ':A' & $i,1 )

      For $y = 1 To 5
      Global $sResultb = _Excel_RangeRead($oWorkbook, Default, 'B' & $y & ':B' & $y,1 )

               WinActivate("[CLASS:IEFrame]", "")
               _IEFormElementSetValue($text_form1,  $sResulta )
               _IEAction ($oInput_btn, "click")
            sleep(5000)
               _IEFormElementSetValue($text_form1,  $sResultb )
               _IEAction ($oInput_btn, "click")
            sleep(5000)


            For $oInput In $oInputs
                  If StringLower($oInput.value) == "check for interactions" Then
                        Global $check_btn = $oInput
                  EndIf
            Next
           _IEAction ($check_btn, "click")


            sleep(5000)

            $oButtonsa = _IETagnameGetCollection($oIE, "span")
            For $oButtonn in $oButtonsa
                If $oButtonn.classname == "status-category status-category-major" Then
                        WinActivate("[CLASS:XLMAIN]", "")
                        _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "major","C" & $y)
                ElseIf $oButtonn.classname == "status-category status-category-moderate" Then
                        WinActivate("[CLASS:XLMAIN]", "")
                        _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "moderate","C" & $y)
                ElseIf $oButtonn.classname == "status-category status-category-minor" Then
                        WinActivate("[CLASS:XLMAIN]", "")
                        _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "minor","C" & $y)
              EndIf
                ExitLoop

            Next
         Next
Next

我已通过最后的润色更新了代码 我认为这段代码现在完成了我在上面写下的步骤 但是当脚本完成第一次迭代时出现错误

【问题讨论】:

  • 结果页面的 URL 是什么样的?您完全有可能直接提交 URL 并获取结果,然后从该页面上刮掉结果。这意味着您可以通过直接提交和抓取数据而不是搜索字段、按钮和东西来做到这一点
  • 不幸的是该链接有 id 我不知道它是指结果链接的示例drugs.com/interactions-check.php?drug_list=917-482,11-2692
  • hmmm 在这种情况下可能会坚持使用 Autoit。我实际上没有在脚本中看到错误消息中的代码。

标签: excel autoit


【解决方案1】:

问题出在 _IEFormElementSetValue 函数中。

大概,

$text_form1 = _IEGetObjById($oIE, "livesearch-interaction")

找不到任何对象。

为了调试它,您可以在 _IEFormElementSetValue 之前插入此代码:

ConsoleWrite("isObject:" & isObj($text_form1) & @CRLF)

更新:我发现了 3 个问题。 1) 迭代结束后没有返回搜索页面,找不到输入对象; 2)您必须在每次迭代开始时获取 Input 和 Button 对象; 3) Exitloop 必须在每个 If ... esleif 部分。

试试这个代码:

#include <Excel.au3>
#include <IE.au3>
#include <File.au3>

Local $sWorkbook = "C:\Users\Aligaaly\Desktop\autoit\test\drugs.xlsx"
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook)
$oWorkbook = _Excel_BookAttach($sWorkbook)


GLOBAL $oIE = _IECreate("https://www.drugs.com/drug_interactions.php")
Local $oInputs = _IETagNameGetCollection($oIE, "input")

For $oInput In $oInputs
      $text_form1 = _IEGetObjById($oIE, "livesearch-interaction")
      If StringLower($oInput.classname) == "input-button search-button" and _IEFormElementGetValue($oInput)  Then ; it is an input
                  Global $oInput_btn = $oInput
      EndIf
Next


WinActivate("[CLASS:XLMAIN]", "")

For $i = 1 To 5
    Global $sResulta = _Excel_RangeRead($oWorkbook, Default, 'A' & $i & ':A' & $i,1 )

          For $y = 1 To 5
          Global $sResultb = _Excel_RangeRead($oWorkbook, Default, 'B' & $y & ':B' & $y,1 )

                $oInputs = _IETagNameGetCollection($oIE, "input")

                For $oInput In $oInputs
                      $text_form1 = _IEGetObjById($oIE, "livesearch-interaction")
                      If StringLower($oInput.classname) == "input-button search-button" and _IEFormElementGetValue($oInput)  Then ; it is an input
                            $oInput_btn = $oInput
                      EndIf
                Next

                   WinActivate("[CLASS:IEFrame]", "")
                   _IEFormElementSetValue($text_form1,  $sResulta )
                   _IEAction ($oInput_btn, "click")
                sleep(5000)
                   _IEFormElementSetValue($text_form1,  $sResultb )
                   _IEAction ($oInput_btn, "click")
                sleep(5000)


                For $oInput In $oInputs
                      If StringLower($oInput.value) == "check for interactions" Then
                            Global $check_btn = $oInput
                      EndIf
                Next
               _IEAction ($check_btn, "click")


                sleep(5000)

                $oButtonsa = _IETagnameGetCollection($oIE, "span")
                For $oButtonn in $oButtonsa
                    If $oButtonn.classname == "status-category status-category-major" Then
                            WinActivate("[CLASS:XLMAIN]", "")
                            _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "major","C" & $y)
                            ExitLoop
                    ElseIf $oButtonn.classname == "status-category status-category-moderate" Then
                            WinActivate("[CLASS:XLMAIN]", "")
                            _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "moderate","C" & $y)
                            ExitLoop
                    ElseIf $oButtonn.classname == "status-category status-category-minor" Then
                            WinActivate("[CLASS:XLMAIN]", "")
                            _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "minor","C" & $y)
                            ExitLoop
                  EndIf      

                Next
                _IENavigate($oIE, "https://www.drugs.com/drug_interactions.php?action=new_list")
             Next
    Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2020-04-23
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多