Using Python to sign into website, fill in a form, then sign out

前端 未结 5 1651
挽巷
挽巷 2021-01-30 09:42

As part of my quest to become better at Python I am now attempting to sign in to a website I frequent, send myself a private message, and then sign out. So far, I\'ve managed to

5条回答
  •  臣服心动
    2021-01-30 09:53

    import urllib
    import urllib2
    
    name =  "name field"
    data = {
            "name" : name 
           }
    
    encoded_data = urllib.urlencode(data)
    content = urllib2.urlopen("http://www.abc.com/messages.php?action=send",
            encoded_data)
    print content.readlines()
    

    just replace http://www.abc.com/messages.php?action=send with the url where your form is being submitted

    reply to your comment: if the url is the url where your form is located, and you need to do this just for one website, look at the source code of the page and find

    and put this address as parameter for urllib2.urlopen

    And you have to realise what submit button does. It just send a Http request to the url defined by action in the form. So what you do is to simulate this request with urllib2

提交回复
热议问题