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!

Community
  • 1
  • 1
Mike Thunder
  • 451
  • 1
  • 9
  • 19
  • 1
    related: [How can I login to a website with Python?](http://stackoverflow.com/questions/2910221/how-can-i-login-to-a-website-with-python) – sloth Feb 25 '13 at 14:45
  • Looks like you've already got lots to go on. What exactly is the problem you're having? – Iguananaut Feb 25 '13 at 14:50

2 Answers2

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)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 3
    I really don't like requests, it's an extra dependency just for saving a couple of lines of code. – LtWorf Feb 25 '13 at 22:18
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.

LtWorf
  • 7,286
  • 6
  • 31
  • 45