1

I trying to login on a website and do automated clean-up jobs.

The site where I need to login is : http://site.com/Account/LogOn

I tried various codes that I found it on Stack, like Login to website using python (but Im stuck on this line

session = requests.session(config={'verbose': sys.stderr}) 

where my JetBeans doesnt like 'verbose' telling me that i need to do something, but doesnt explain exactly what).

I also tried this: Browser simulation - Python, but no luck with this too.

Can anyone help me? All answers will be appreciate. Thanks in advance.

PS: I started learning Python 2 weeks ago so please elaborate your answer for my "pro" level of undersanding :)

-------------------------UPDATE:-----------------------------

I manage to login, but when I'm trying to move on other page and push a button, it says Please Log in!

I use this code:

url = 'http://site.com/Account/LogOn'
values = {'UserName': 'user',
          'Password': 'pass'}

data = urllib.urlencode(values)
cookies = cookielib.CookieJar()

opener = urllib2.build_opener(
    urllib2.HTTPRedirectHandler(),
    urllib2.HTTPHandler(debuglevel=0),
    urllib2.HTTPSHandler(debuglevel=0),
    urllib2.HTTPCookieProcessor(cookies))

response = opener.open(url, data)
the_page = response.read()
http_headers = response.info()
print response

After I log in I need to swith a menu value, that looks like this in HTML:

<select id="menu_uid" name="menu_uid" onchange="swapTool()" style="font-size:8pt;width:120px;">
<option value="1" selected>MyProfile</option>
...
<option value="6" >DeleteTree</option>

but I also can do it directly if I form a URL like this: http://site.com/Account/management.html?Category=6&deltreeid=6&do=Delete+Tree

So , how can I build this URL and submit it? Thanks again!

2
  • 1
    related: How can I login to a website with Python? Commented Feb 25, 2013 at 14:45
  • Looks like you've already got lots to go on. What exactly is the problem you're having? Commented Feb 25, 2013 at 14:50

2 Answers 2

4

Save yourself a lot of headache and use requests:

url = 'http://site.com/Account/LogOn'
values = {'UserName': 'user',
          'Password': 'pass'}

r = requests.post(url, data=values)
# Now you have logged in

params = {'Category': 6, 'deltreeid': 6, 'do': 'Delete Tree'}
url = 'http://site.com/Account/management.html'

# sending cookies as well
result = requests.get(url, data=params, cookies=r.cookies)
Sign up to request clarification or add additional context in comments.

1 Comment

I really don't like requests, it's an extra dependency just for saving a couple of lines of code.
1

Well 1st things

it sends a POST request to /Account/LogOn. The fields are called UserName and Password.

Then you can use python's httplib to do HTTP requests

http://docs.python.org/2/library/httplib.html

(There is an example in the end on how to do a POST).

Then you will get a response containing a session cookie probably, within a HTTP header. You need to store that cookie in a variable and send it in all the subsequent requests to be authenticated.

1 Comment

No need for further help I made it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.