How to pull specific image URL from webpage HTML and insert into excel

PorterSLC 0 Reputation points
2024-01-14T03:18:37.22+00:00

I have a spreadsheet with over 1000 rows containing product information. Each row is different product. Column D is the CONCATENATED URL to the product webpage. I need to return a specific image URL to each cell in column E that is found in the HTML of the product URL from column D. The image URL that I need to return to column E is always located immediately after the following HTML code on the product page: <img class="zoom-image" data-bind="click: ShowGalleryModal, attr: { src: CurrentImage.src }" src=" and immediately before the fallowing HTML code: "> <small>Roll over image to zoom in</small> However, the file names are not always the same number of characters, so I don't believe I'm able to use a string that will count characters. Assuming I had the correct string to search for these codes and extract the URL from between them, I don't quite know how to implement it into excel. Any help is greatly appreciated. Thank you!

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Azar 29,520 Reputation points MVP Volunteer Moderator
    2024-01-14T03:42:29.37+00:00

    Hey PorterSLC,

    I guess you can use text function here to find the starting and ending positions of the image URL in the HTML.

    For example, you might use FIND to find the starting position and MID to extract the substring.

    Search for the HTML Tags:

    • Use FIND to locate the position of the HTML tags <img class="zoom-image" data-bind="click: ShowGalleryModal, attr: { src: CurrentImage.src }" src=" and "> <small>Roll over image to zoom in</small>.

    Extract the URL:

    • Once you have the positions, use MID to extract the substring between these positions, which would be the image URL.
    =MID(D2, FIND("<img class=""zoom-image"" data-bind=""click: ShowGalleryModal, attr: { src: CurrentImage.src }"" src=""", D2) + LEN("<img class=""zoom-image"" data-bind=""click: ShowGalleryModal, attr: { src: CurrentImage.src }"" src=""") + 1, FIND("""> <small>Roll over image to zoom in</small>", D2) - (FIND("<img class=""zoom-image"" data-bind=""click: ShowGalleryModal, attr: { src: CurrentImage.src }"" src=""", D2) + LEN("<img class=""zoom-image"" data-bind=""click: ShowGalleryModal, attr: { src: CurrentImage.src }"" src=""") + 1))
    
    
    

    you might need to use VBA. a custom macro to handle the HTML parsing

    Function ExtractImageURL(html As String) As String     Dim startTag As String     Dim endTag As String     Dim startPos As Long     Dim endPos As Long      startTag = "<img class=""zoom-image"" data-bind=""click: ShowGalleryModal, attr: { src: CurrentImage.src }"" src="""     endTag = """> <small>Roll over image to zoom in</small>"      startPos = InStr(html, startTag)     endPos = InStr(startPos, html, endTag)      If startPos > 0 And endPos > 0 Then         ExtractImageURL = Mid(html, startPos + Len(startTag), endPos - startPos - Len(startTag))     Else         ExtractImageURL = "URL Not Found"     End If End Function 
    

    You can call the func in excel like this - =ExtractImageURL(D2)

    If this helps kindly accept the answer thanks much.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.