VBA:在IE中搜索文本,打开链接,查找行复制文本,在Excel中插入

时间:2019-05-28 13:00:05

标签: excel vba internet-explorer

我是一个完全的新手。你们可以在以下方面帮助我吗? 1.我有一个包含产品代码的电子表格。 2.我在索引页面上有一个活动的Internet Explorer会话(已登录),该页面上充满了包含上述产品代码的链接。 3.对于每个代码,我都需要它来找到代码,打开链接。然后找到包含字符串“ udsalg”的行并复制完整的行,即“ Udsalg 10994” 4.在产品代码旁边插入字符串。 5.输入IE,然后“返回”到索引页面。 5.冲洗并重复直到搜索完所有产品代码。

我对此没有技巧,希望获得帮助。我了解基本编程(php等)。我希望有人可以帮助或指出正确的方向。最好的问候。

1 个答案:

答案 0 :(得分:0)

您可以尝试参考下面的代码示例,它将为您提供上述问题的大部分答案。请注意,这不是确切的代码。

'start a new subroutine called SearchBot
Sub SearchBot()

    'dimension (declare or set aside memory for) our variables
    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
    Dim y As Integer 'integer variable we'll use as a counter
    Dim result as String 'string variable that will hold our result link

    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorer

    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True 

    'navigate IE to this web page (a pretty neat search engine really) 
    objIE.navigate "https://duckduckgo.com" 

    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

    'in the search box put cell "A2" value, the word "in" and cell "C1" value 
    objIE.document.getElementById("search_form_input_homepage").Value = _ 
      Sheets("Sheet1").Range("A2").Value & " in " & Sheets("Sheet1").Range("C1").Value

    'click the 'go' button 
    objIE.document.getElementById("search_button_homepage").Click

    'wait again for the browser 
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

    'the first search result will go in row 2 
    y = 2 

    'for each <a> element in the collection of objects with class of 'result__a'... 
    For Each aEle In objIE.document.getElementsByClassName("result__a") 

        '...get the href link and print it to the sheet in col C, row y 
        result = aEle
        Sheets("Sheet1").Range("C" & y).Value = result

        '...get the text within the element and print it to the sheet in col D
        Sheets("Sheet1").Range("D" & y).Value = aEle.innerText
        Debug.Print aEle.innerText

        'is it a yellowpages link?
        If InStr(result, "yellowpages.com") > 0 Or InStr(result, "yp.com") > 0 Then
            'make the result red
            Sheets("Sheet1").Range("C" & y).Interior.ColorIndex = 3
            'place a 1 to the left
            Sheets("Sheet1").Range("B" & y).Value = 1
        End If

        'increment our row counter, so the next result goes below 
        y = y + 1 

    'repeat times the # of ele's we have in the collection 
    Next

    'add up the yellowpages listings
    Sheets("Sheet1").Range("B1").Value = _
      Application.WorksheetFunction.Sum(Sheets("Sheet1").Range("B2:B100"))

    'close the browser
    objIE.Quit

'exit our SearchBot subroutine
End Sub

您可以尝试参考代码并尝试理解它。然后,您可以尝试根据需要对其进行修改。如果您还有其他问题,可以尝试发布示例代码。我们将尝试检查它并尝试为您提供建议。

有关IE VBA自动化的有用参考。

(1)Excel + VBA + IE = web automation

(2)IE (Internet Explorer) Automation using Excel VBA

(3)Automate Internet Explorer (IE) Using VBA