+ Reply to Thread
Results 1 to 10 of 10

a code to pull data from a website

Hybrid View

  1. #1
    Registered User
    Join Date
    12-29-2012
    Location
    Latakia, Syria
    MS-Off Ver
    Excel 2010
    Posts
    61

    a code to pull data from a website

    Hi geniuses!
    In a cell B2 of a worksheet, the user enters a "doi" value (that is like an ID for any publication!). I am trying to find a code that translates this "doi" to a citation that I can use in my thesis. after a long search, I found a website that does this for me. this website is "https://citation.crosscite.org/". you simply need to insert the doi value (the content of B2 in my worksheet) in the designated box, hit "format" and you will get the citation on the same webpage. is there a way to automate this in excel by a vba? I know that is called "web scraping" but I wasn't able to make it to work in my case. any help will be super appreciated! (ps, you will be helping a scientist who is trying to kick cancer's ***!)

    Thanks a lot!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,155

    Re: a code to pull data from a website

    The below code will do the job;

    Sub GetData()
        ' Haluk-25/09/2021
        ' https://excelhaluk.blogspot.com/
        ' ---------------------------------------
        '
        Dim NoB As Long, i As Long, HTTP As Object
        Dim URL As String
        
        Range("C3:C" & Rows.Count).ClearContents
        
        NoB = Range("B" & Rows.Count).End(xlUp).Row
        
        Set HTTP = CreateObject("MSXML2.XMLHTTP")
        
        For i = 3 To NoB
            URL = "https://citation.crosscite.org/format?doi=" & URL_Encode(Range("B" & i)) & "&style=apa&lang=en-US"
            HTTP.Open "GET", URL, False
            HTTP.send
            
            If HTTP.Status = 200 Then
                Range("C" & i) = HTTP.responseText
            Else
                Range("C" & i) = "Not Found!"
            End If
        Next
        
        Set HTTP = Nothing
    End Sub
    '
    Function URL_Encode(strText)
        Dim objHtmlfile As Object
        
        If objHtmlfile Is Nothing Then
            Set objHtmlfile = CreateObject("HTMLFILE")
            objHtmlfile.parentWindow.execScript "function encode(s) {return encodeURIComponent(s);}", "JScript"
        End If
        
        URL_Encode = objHtmlfile.parentWindow.encode(strText)
    End Function
    Last edited by Haluk; 09-25-2021 at 01:05 PM.

  3. #3
    Registered User
    Join Date
    12-29-2012
    Location
    Latakia, Syria
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: a code to pull data from a website

    that is absolutely great!!!! I can't thank you enough!
    however, I have a question. I want to adapt this amazing code to work even if my B column (doi entries) contains thousands of entries/some cells are even empty. also, can I activate the code automatically in real-time? so, whenever a user enters a new entry into the doi column, it searches only for the new entry & returns the results in a matching cell in column d for example?
    thanks from all my heart!

  4. #4
    Registered User
    Join Date
    12-29-2012
    Location
    Latakia, Syria
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: a code to pull data from a website

    is there also a way to ask the webpage to retrieve the citations with a different style than the default (apa) in the code? if there is a way, it would be super awesome! :D

  5. #5
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,155

    Re: a code to pull data from a website

    Thanks for the kind words....

    You may try the revised below code which works in range B3:B50000

    You can define the range bigger or smaller to suit your needs.


    You need to enter this in Sheet1 code module;

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("B3:B50000")) Is Nothing Then
            If Target <> "" Then
                Call GetReference(Target)
            End If
        End If
    End Sub
    And, enter the following code lines in a standart module;

    Sub GetReference(myRng As Range)
        ' Haluk-25/09/2021
        ' https://excelhaluk.blogspot.com/
        ' ---------------------------------------
        '
        Dim HTTP As Object
        Dim URL As String
            
        Set HTTP = CreateObject("MSXML2.XMLHTTP")
        
        URL = "https://citation.crosscite.org/format?doi=" & URL_Encode(myRng) & "&style=apa&lang=en-US"
        HTTP.Open "GET", URL, False
        HTTP.send
        
        If HTTP.Status = 200 Then
            myRng.Offset(0, 1) = HTTP.responseText
        Else
            myRng.Offset(0, 1) = "Not Found!"
        End If
        
        Set HTTP = Nothing
    End Sub
    '
    Function URL_Encode(strText)
        Dim objHtmlfile As Object
        
        If objHtmlfile Is Nothing Then
            Set objHtmlfile = CreateObject("HTMLFILE")
            objHtmlfile.parentWindow.execScript "function encode(s) {return encodeURIComponent(s);}", "JScript"
        End If
        
        URL_Encode = objHtmlfile.parentWindow.encode(strText)
    End Function

    Note: Sample workbook is also attached...
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    12-29-2012
    Location
    Latakia, Syria
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: a code to pull data from a website

    that is super great! I have to celebrate! thanks a looooot!

  7. #7
    Registered User
    Join Date
    12-29-2012
    Location
    Latakia, Syria
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: a code to pull data from a website

    Hi Marc L,
    Thanks for your answer. However, I didn't even know how to read let alone apply this super-advanced code! could you please, explain to me how I can apply it?
    Thanks!

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: a code to pull data from a website

    Quote Originally Posted by blue_clouds_mountain View Post
    I didn't even know how to read let alone apply this super-advanced code! could you please, explain to me how I can apply it?
    As it is at the same level than Haluk's initial code, no more advanced …
    It was just an answer to your initial post to process all rows at once.
    Thanks for the rep' !

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    According to the attachment a VBA demonstration for starters :

    PHP Code: 
    Sub Demo1()
            
    Dim VR&
        
    With Range("B3:C" & [B1].CurrentRegion.Rows.Count)
            
    Application.Index(.CellsEvaluate("ROW(1:" & .Rows.Count ")"), [{2,1}])
        
    With CreateObject("WinHttp.WinHttpRequest.5.1")
                
    On Error Resume Next
            
    For 1 To UBound(V)
                If 
    IsEmpty(V(R1)) And Not IsEmpty(V(R2)) Then
                   
    .Open "GET""https://citation.crosscite.org/format?style=apa&lang=en-US&doi=" V(R2), False
                   
    .setRequestHeader "DNT""1"
                   
    .send
                    
    If .Status 200 Then V(R1) = .responseText
                End 
    If
            
    Next
        End With
           
    .Columns(2).Value2 V
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  10. #10
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,155

    Re: a code to pull data from a website

    You're welcome and thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pull data from website?
    By nobrown in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2016, 08:19 AM
  2. Pull Website Source Code with Selenium (PhantomJS) VBA
    By mattmc419 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-18-2015, 05:02 AM
  3. Trying to pull data from website
    By SHUTTEHFACE in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2014, 12:28 PM
  4. [SOLVED] VBA to Pull Data from Log in Website
    By rlee12 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-09-2013, 10:31 AM
  5. Pull Data from website.. can it be done?
    By sniderza in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-05-2012, 01:22 PM
  6. How can I pull the data next to a name from a website in VBA?
    By max3732 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-06-2010, 10:29 AM
  7. Pull data from website
    By djboston in forum Excel General
    Replies: 0
    Last Post: 01-02-2010, 01:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1