1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

    Yours,
    Chandoo
  2. 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...

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

Post method on form

Discussion in 'VBA Macros' started by YasserKhalil, Sep 8, 2017.

  1. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    Hello everyone
    I have this link
    http://www.res-ye.net
    There are two forms with input text with the same data
    <input type="text" name="num" id="i_text">

    How can I determine any of them to deal with .. I need to put a number then clicking "Submit" button
  2. Marc L

    Marc L Excel Ninja

    Messages:
    3,277

    Hi !

    You can deal with a parent of the element, for example the Forms collection
    and index 0 or 1 for the first two elements …
  3. shahin

    shahin Active Member

    Messages:
    598
  4. shahin

    shahin Active Member

    Messages:
    598
    Here is the script. If you run it, you will get the table with data from that page.
    Code (vb):

    Sub httpPost()

        Dim http As New XMLHTTP60, html As New HTMLDocument
        Dim htmla As Object, trow As Object, tcel As Object, ArgumentStr As String

        ArgumentStr = "grade=1&num=23&submit=Search"

        With http
            .Open "POST", "http://www.res-ye.net/class2.php", False
            .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
            .send ArgumentStr
            html.body.innerHTML = .responseText
        End With

        Set htmla = html.getElementsByTagName("table")(2)
     
        For Each trow In htmla.Rows
            For Each tcel In trow.Cells
                c = c + 1: Cells(x + 1, c) = tcel.innerText
            Next tcel
            c = 0
            x = x + 1
        Next trow

    End Sub
     

    Btw, in this very line ArgumentStr = "grade=1&num=23&submit=Search", you can see there is a parameter num=23. If you change the number, you will get different tables. Hope you got me. Thanks.
    NARAYANK991 and YasserKhalil like this.
  5. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    That's wonderful and great Shahin. You are superstar at scraping
    Can you guide me to resources to learn more about scraping?

    As for the thread I need a specific point in fact ..
    To scrape the name next to this string
    الاســـــــم :
    And the string next to
    النتيجة النهائية
    and the string next to المعدل
    Each of these results would be put in columns B:D and in A will be the number (39) for example

    Thank you very much for great help
  6. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    Any help in the point I refer to Mr. Shahin
    Sorry for disturbing you. I appreciate your help a lot
  7. shahin

    shahin Active Member

    Messages:
    598
    As long as the above quotation is concerned, gurus like Marc L, Chihiro and Narayan are second to none. So, don't make them angry. I'm working on a project at this moment. However, as soon as I have time, I'll take a look. Thanks.
    YasserKhalil likes this.
  8. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    In fact all of the names you mentioned (Marc L, Chihiro and Narayan ) are superstars and no doubt and that doesn't lower their rank at all
    I just named you because you started the help and take your time ..
    I will be waiting for you my friend
  9. shahin

    shahin Active Member

    Messages:
    598
    One more thing: could you please clarify the string names in English as you did in Arabic. At the right most corner in the chrome address bar there is an icon in which if you hover your mouse you will see an option for translation popping up. Just press that.
  10. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    Thanks a lot fore reply
    The strings are in sequence: sitting number / Name: / Final Result / Rate
  11. shahin

    shahin Active Member

    Messages:
    598
    Hey there!!! Give this a try. It will generate results based on your provided sitting number and gets placed in it's right position. I've used offset property so no loop is required here. Thanks.
    Code (vb):

    Sub result_generator()

        Dim http As New XMLHTTP60, html As New HTMLDocument, ArgumentStr As String
        Dim posts As Object, post As Object, elems As Object, elem As Object
       
        [B1:E1].Value = [{"Sitting Number","Name","Final Result","Rate"}]
        ArgumentStr = "grade=1&num=24&submit=Search"
        With http
            .Open "POST", "http://www.res-ye.net/class2.php", False
            .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
            .send ArgumentStr
            html.body.innerHTML = .responseText
        End With

        Set elems = html.getElementsByTagName("td")
        ActiveCell.Offset(1, 1) = elems(1).innerText
        Set posts = html.getElementById("success").getElementsByTagName("h3")
        ActiveCell.Offset(1, 2) = posts(0).innerText
        Set elem = html.getElementsByClassName("na")(0).getElementsByTagName("span")
        ActiveCell.Offset(1, 3) = elem(1).innerText
        Set post = html.getElementsByClassName("na")(0).getElementsByTagName("span")
        ActiveCell.Offset(1, 4) = post(2).innerText
        ActiveCell.Offset(1, 0).Select
           
    End Sub
     
    Btw, just tell me this is not what you wanted.
    Last edited: Sep 10, 2017
  12. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    You're awesome and wonderful
    Thank you very much for great help

    Can you give me some powerful resources to learn the scraping from?
  13. shahin

    shahin Active Member

    Messages:
    598
    Hey this attachment is the right one. Make sure to press the start over button before pressing the "press button". That's it.

    Attached Files:

  14. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    Thank you very very much Mr. Shahin
    You have helped me a lot. As for dealing with the worksheet and cells I could change these tiny matters. You have done the essence and the most important part for me
    Thanks a lot .. Best Regards
  15. shahin

    shahin Active Member

    Messages:
    598
    No problem. Stay well.
    YasserKhalil likes this.
  16. YasserKhalil

    YasserKhalil Active Member

    Messages:
    774
    I have to thank you again and again for your great efforts and awesome help
    Best and kind regards

Share This Page