在关键词旁边插入Google中的第一张图片。

huangapple go评论79阅读模式
英文:

Find first image in google and insert next to keyword

问题

我试图找到一个脚本,根据关键字插入第一张图片,例如,A1中的“Abelia chinensis Variegata”会找到该植物的图片并将其放在B1中。

我已经找到了下面的脚本。

尽管关键字位于列D中,但在A中的结果返回为零。

因此无法检查它是否满足我的需求。

我觉得我离成功很近,任何帮助将不胜感激。

Public Sub imagedownload()
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim imgElements As IHTMLElementCollection
    Dim imgElement As HTMLImg
    Dim aElement As HTMLAnchorElement
    Dim n As Integer, I As Integer
    Dim Url As String, url2 As String
    Dim m, LastRow As Long
    Dim furl As String
    Sheets("one").Select
    LastRow = Range("D" & Rows.Count).End(xlUp).Row
    For I = 3 To LastRow
        Url = "https://www.google.com/search?q=" & Cells(I, 4) & "&source=lnms&tbm=isch&sa=X&rnd=1"
        Set IE = New InternetExplorer
        On Error Resume Next
        Sheets("one").Select
        With IE
            .Visible = False
            .navigate Url 'sWebSiteURL
            Do Until .readyState = 4: DoEvents: Loop
            'Do Until IE.document.readyState = "complete": DoEvents: Loop
            Set HTMLdoc = .document
            Set imgElements = HTMLdoc.getElementsByTagName("IMG")
            n = 1
            For Each imgElement In imgElements
                On Error Resume Next
                If InStr(imgElement.src, sImageSearchString) Then
                    If imgElement.ParentNode.nodeName = "A" Then
                        Set aElement = imgElement.ParentNode
                        'Cells(n, 2).Value = imgElement.src
                        'Cells(n, 3).Value = aElement.href
                        If n = 2 Then
                            url2 = aElement.href 'imgElement.src
                            url3 = imgElement.src 'aElement.href
                            GoTo done:
                        End If
                        n = n + 1
                    End If
                End If
            Next
done:
            furl = InStrRev(url2, "&imgrefurl=", -1)
            furl = Mid(url2, 37, furl - 37)
            Sheets("two").Select
            'On Error Resume Next
            Cells(I, 1) = furl
            Set m = ActiveSheet.Pictures.Insert(furl)
            With Cells(I, 1)
                t = .Top
                l = .Left
                w = .Width
                h = .Height
            End With
            With m
                .Top = t
                .Left = l
                .ShapeRange.Width = w
                .ShapeRange.Height = h
            End With
            Sheets("one").Select
            IE.Quit
            Set IE = Nothing
        End With
    Next
End Sub

希望是返回图片,即使与关键字D中的每个关键字不匹配。

英文:

Im trying to find a script that inserts the first image based on a keyword i.e. A1 "Abelia chinensis Variegata" finds a picture of the plant and puts it in B1

I have found the script below

Although the keyword is column D the results in A thy are returning zeros against each search

So unable to check if this does what i need it to.

I feel I'm so close, any help would be greatly appreciated.

Public Sub imagedownload()
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim imgElements As IHTMLElementCollection
    Dim imgElement As HTMLImg
    Dim aElement As HTMLAnchorElement
    Dim n As Integer, I As Integer
    Dim Url As String, url2 As String
    Dim m, LastRow As Long
    Dim furl As String
    Sheets("one").Select
    LastRow = Range("D" & Rows.Count).End(xlUp).Row
    For I = 3 To LastRow
        Url = "https://www.google.com/search?q=" & Cells(I, 4) & "&source=lnms&tbm=isch&sa=X&rnd=1"
        Set IE = New InternetExplorer
        On Error Resume Next
        Sheets("one").Select
        With IE
            .Visible = False
            .navigate Url 'sWebSiteURL
            Do Until .readyState = 4: DoEvents: Loop
            'Do Until IE.document.readyState = "complete": DoEvents: Loop
            Set HTMLdoc = .document
            Set imgElements = HTMLdoc.getElementsByTagName("IMG")
            n = 1
            For Each imgElement In imgElements
                On Error Resume Next
                If InStr(imgElement.src, sImageSearchString) Then
                    If imgElement.ParentNode.nodeName = "A" Then
                        Set aElement = imgElement.ParentNode
                        'Cells(n, 2).Value = imgElement.src
                        'Cells(n, 3).Value = aElement.href
                        If n = 2 Then
                            url2 = aElement.href 'imgElement.src
                            url3 = imgElement.src 'aElement.href
                            GoTo done:
                        End If
                        n = n + 1
                    End If
                End If
            Next
done:
            furl = InStrRev(url2, "&imgrefurl=", -1)
            furl = Mid(url2, 37, furl - 37)
            Sheets("two").Select
            'On Error Resume Next
            Cells(I, 1) = furl
            Set m = ActiveSheet.Pictures.Insert(furl)
            With Cells(I, 1)
                t = .Top
                l = .Left
                w = .Width
                h = .Height
            End With
            With m
                .Top = t
                .Left = l
                .ShapeRange.Width = w
                .ShapeRange.Height = h
            End With
            Sheets("one").Select
            IE.Quit
            Set IE = Nothing
        End With
    Next
End Sub

The hope was to return an image even if wrong against each keyword in D

答案1

得分: 2

I've hacked away at your code and got it to work. It really needs a full re-write to be honest.

Please note: I've removed the need for a second sheet for testing purposes, but it should be fairly simple for you to put that back in.

Public Sub imagedownload()
    ' Your VBA code here
End Sub

I'm not sure what your code was doing, but it appeared to be looking for perhaps an old style URL make-up that Google no longer use. Note: the code above works today, but Google can change the way their search engine builds its pages at any time.

英文:

I've hacked away at your code and got it to work. It really needs a full re-write to be honest.

Please note: I've removed the need for a second sheet for testing purposes, but it should be fairly simple for you to put that back in.

Public Sub imagedownload()
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim imgElements As IHTMLElementCollection
    Dim imgElement As HTMLImg
    Dim aElement As HTMLAnchorElement
    Dim n As Integer, I As Integer
    Dim Url As String, url2 As String
    Dim m, LastRow As Long
    Dim furl As String
    Sheets("one").Select
    LastRow = Range("D" & Rows.Count).End(xlUp).Row
    For I = 3 To LastRow
        sImageSearchString = Cells(I, 4)
        Url = "https://www.google.com/search?q=" & sImageSearchString & "&source=lnms&tbm=isch&sa=X&rnd=1"
        Set IE = New InternetExplorer
        On Error Resume Next
        Sheets("one").Select
        With IE
            .Visible = True
            .navigate Url 'sWebSiteURL
            Do Until .readyState = 4: DoEvents: Loop
            'Do Until IE.document.readyState = "complete": DoEvents: Loop
            Set HTMLdoc = .document
            Set imgElements = HTMLdoc.getElementsByTagName("IMG")
            furl = ""
            For Each imgElement In imgElements
                On Error Resume Next
                If furl = "" And InStr(imgElement.src, "gstatic") Then
                    furl = imgElement.src
                End If
            Next

            Cells(I, 6) = furl
            Set m = ActiveSheet.Pictures.Insert(furl)
            With Cells(I, 5)
                t = .Top
                l = .Left
                w = .Width
                h = .Height
            End With
            With m
                .Top = t
                .Left = l
                .ShapeRange.Width = w
                .ShapeRange.Height = h
            End With
            IE.Quit
            Set IE = Nothing
        End With
    Next
End Sub

Input:

在关键词旁边插入Google中的第一张图片。

Output:

在关键词旁边插入Google中的第一张图片。

I'm not sure what your code was doing, but it appeared to be looking for perhaps an old style URL make-up that Google no longer use. Note: the code above works today, but Google can change the way their search engine builds its pages at any time.

huangapple
  • 本文由 发表于 2023年6月27日 20:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定