Python: Login on a website

倾然丶 夕夏残阳落幕 提交于 2019-12-23 02:38:15

问题


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!


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/15069524/python-login-on-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!