• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

PGN download from chess.com

This is my try but got an error
Code:
Sub DownloadPGNChessFile()
    Dim ie As InternetExplorer
   
    Set ie = New InternetExplorer
    ie.navigate "https://www.chess.com/tactics/481868"
    ie.Visible = True
   
    While ie.busy: DoEvents: Wend
   
    ie.document.all("shareBoard(true)").Click
   
    MsgBox "OK"
End Sub
 
Thanks a lot for reply Mr. Hui
This is not a cross-post at all .. Have a look and you will find that both threads are different ...
 
There's no simple way to do it. The site requires that you solve the puzzle/scenario before download becomes available.
 
That's ok.. I don't intend to download before solving it ..
The idea is that I need to download multiple pgn files by looping through the tactics numbers in column A and these are already solved
Can you solve it and try downloading using VBA?
 
Thanks a lot for reply. But how did you get this link in your post #8?
Can you teach me how did you get it?

And this is a link to an image not to the file itself
 
Ah, I misread "PGN" as "PNG".

In that case, try something like below. You should see the info you are looking for in the print out.

Code:
Sub DownloadPGNChessFile()
    Dim ie As InternetExplorer
    Dim x As Object
    Set ie = New InternetExplorer
    ie.navigate "https://www.chess.com/tactics/481868"
    ie.Visible = True

    While ie.Busy: DoEvents: Wend

    Set x = ie.document.getElementsByTagName("button")
    For Each y In x
        If InStr(1, y.innerHTML, "icon-download") > 0 Then
            y.Click
            Exit For
        End If
    Next

    While ie.Busy: DoEvents: Wend

    Set x = ie.document.getElementsByTagName("div")
    For Each y In x
        If InStr(1, y.innerHTML, "chess_com_tactics_board_ShareMenuPgnContentTextareaId") > 0 Then
        Debug.Print y.innerHTML
            Exit For
        End If
    Next

End Sub

FYI - From what I can tell. PGN is just text file using specific notation.

Edit: Do further string manipulation and use any number of method to output result to text file.
 
Last edited:
Thanks a lot Mr. Chihiro for this great start point
I executed the code but didn't get anything in Immediate window but after using F8 to run it line by line I could get the strings in Immediate Window but I think it is the source code of the webpage not PGN notation
 

Attachments

Hmm, try this then. Change oPath as needed.
Code:
Sub DownloadPGNChessFile()
    Dim ie As InternetExplorer
    Dim x As Object
    Dim myString As String
    Set ie = New InternetExplorer
    ie.navigate "https://www.chess.com/tactics/481868"
    ie.Visible = True

    While ie.Busy: DoEvents: Wend

    Set x = ie.document.getElementsByTagName("button")
    For Each y In x
        If InStr(1, y.innerHTML, "icon-download") > 0 Then
            y.Click
            Exit For
        End If
    Next
    While ie.Busy: DoEvents: Wend
    Set x = ie.document.getElementsByTagName("div")
    For Each y In x
        If InStr(1, y.innerHTML, "[Full") > 0 Then
            myString = Split(Split(y.innerHTML, "init('")(1), "'https://www.chess.com/tactics'")(0)
            Dim oPath As String: oPath = "C:\Test\myPGN.Text"
            Dim intFF As Integer: intFF = FreeFile()
            Open oPath For Output As #intFF
            Print #intFF, myString
            Close #intFF
            Exit For
        End If
    Next

End Sub

You will find the info you are looking for in there (I think there are some extra info).

Since the site uses API to upload the data, you really don't have direct way of downloading PGN. You will need to loop through elements and find whatever element that holds the info you need and extract it out.

However, it looks like it's URL encoded, so you'll need to convert it a bit. See link (not tested).
http://www.freevbcode.com/ShowCode.asp?ID=1512

EDIT: Oh wait. I think you might be able to loop through element again and click Download. You just need to study the source code and find the correct element to look for.
 
Thank you very much Mr. Chihiro for your great efforts ..
It seems it is difficult to solve it directly ...
The text file produced I can't deal with it at all ...

Is it possible to parse needed data from this text file?
This is the original file .. It can be opened with Notepad ++
 

Attachments

Try this.

Code:
Sub DownloadPGNChessFile()
    Dim ie As InternetExplorer
    Dim x As Object
    Dim myString As String
    Set ie = New InternetExplorer
    ie.navigate "https://www.chess.com/tactics/481868"
    ie.Visible = True

    While ie.Busy: DoEvents: Wend

    Set x = ie.document.getElementsByTagName("button")
    For Each y In x
        If InStr(1, y.innerHTML, "icon-download") > 0 Then
            y.Click
            Exit For
        End If
    Next
    While ie.Busy: DoEvents: Wend
    Set x = ie.document.getElementsByTagName("div")
    For Each y In x
        If InStr(1, y.innerHTML, "chess_com_tactics_board_ShareMenuPgnContentTextareaId") > 0 Then
            myString = Split(Split(y.innerText, "PGN")(1), "Download")(0)
            Dim oPath As String: oPath = "C:\Test\myPGN.pgn"
            Dim intFF As Integer: intFF = FreeFile()
            Open oPath For Output As #intFF
            Print #intFF, myString
            Close #intFF
            Exit For
        End If
    Next

End Sub
 
Last edited:
That's really wonderful. Exactly as needed ..
But just a question : why does it work fine only when using F8 key while it doesn't work properly when executing it in one shot?
Can we do without navigating the url and obtain the html page in another way that would be faster?
 
why does it work fine only when using F8 key while it doesn't work properly when executing it in one shot?
No idea. I didn't need to step through it (I usually never do).

As for your other question, may be. But no clue how as there's API used in the site that uploads the info to some location I can't trace. And then uses that info to create PGN. Above is the simplest method I can think of.
 
Hi ,

Have you tried this :
Code:
x(15).Click
instead of this :
Code:
    For Each y In x
        If InStr(1, y.innerHTML, "icon-download") > 0 Then
            y.Click
            Exit For
        End If
    Next
Narayan
 
Nope. I am lazy and since it's single loop of fairly small number of elements... I didn't bother digging ;)

But that should work better :)
 
This worked for me.
Code:
Sub DownloadPGNChessFile()
    Dim ie As InternetExplorer
    Dim x As Object
    Dim myString As String
    Set ie = New InternetExplorer
    ie.navigate "https://www.chess.com/tactics/481868"
    ie.Visible = True

    While ie.Busy: DoEvents: Wend

    Set x = ie.document.getElementsByTagName("button")
    x(15).Click
    While ie.Busy: DoEvents: Wend
    Set x = ie.document.getElementsByTagName("div")
    For Each y In x
        If InStr(1, y.innerHTML, "chess_com_tactics_board_ShareMenuPgnContentTextareaId") > 0 Then
            myString = Split(Split(y.innerText, "PGN")(1), "Download")(0)
            Dim oPath As String: oPath = "C:\Test\myPGN.pgn"
            Dim intFF As Integer: intFF = FreeFile()
            Open oPath For Output As #intFF
            Print #intFF, myString
            Close #intFF
            Exit For
        End If
    Next

End Sub

Edit: Or remove "Set x=..("button")" line and replace with...
Code:
ie.document.getElementsByTagName("button")(15).Click
 
Back
Top