Python: Login on a website

好久不见. 提交于 2019-12-06 19:37:25

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)

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.

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