0

If I try to login to my account and it is not successful then I would have phrase try again in url source page. So I tried to write python script to login to my account and do stuff:

Update

token=...#by xpath
session=requests.Session('http://example.com')
response=session.get('http://example.com')
cook=session.cookies
postdata={'token':token, 'arg1':'', 'arg2':'', 'name[user]': user, 'name[password]':password, 'arg3': 'Sign in'}
postresp=requests.post(url='http://example.com/sth', cookies=cook, data=post_data)
print postresp.content

Is there something wrong with postdata or etc?

I also sat cookies.

5
  • 2
    Perhaps some cookie is necessary. Commented Nov 19, 2014 at 10:29
  • @laike9m Ok...You mean I am not wrong, are I? Commented Nov 19, 2014 at 14:28
  • There's no right or wrong, just work or not work, you have to keep trying before it gets to work. Commented Nov 19, 2014 at 14:33
  • @MortezaLSC Can I have the url please? Please tag my name in your comment while replying. Thanks :-) Commented Nov 21, 2014 at 6:28
  • @Md. Mohsin I really like Indian friends... pardon me..I am not erally keen on showing url.... Commented Nov 21, 2014 at 8:28

2 Answers 2

1

You don't want to show the url - and its difficult to hit the bulls eye without it:-)

Here's my try let me know if it works or please comment with the error you get here after.

Please note the following points: -try to use a user agent in the headers -Token needs to be fetched after calling the url (In the following case its the 5th line)

session=requests.Session()
headers={"User-Agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"}
session.headers.update(headers)
response=session.get('http://example.com').content #I added this line
tree=html.fromstring(cont)                         #and this line 
token=tree.xpath('//*[@class="blah blah blah"]')
#cook=session.cookies -- You don't need this if you are continuing with the same session
postdata={'token':token, 'arg1':'', 'arg2':'', 'name[user]': user, 'name[password]':password, 'arg3': 'Sign in'}
postresp=session.post(url='http://example.com/sth', data=post_data) # use session.post instead of making a completely new request
print postresp.content

Also, please check if there is specific content-type if yes, then please add it to the headers

Hope that helps :-)

Sign up to request clarification or add additional context in comments.

2 Comments

Thaaaaaaaaaaaaaaaaanxxxxxxxxxxxxx
@MortezaLSC session.get is to use the same session throughout - the website might be using specific tokens for every session. So session.get and session.post uses the same session throughout. Not needing us to carry forward the cookies. And even avoids the token from being changed. On the other hand requests.get opens a new one time session altogther - so all cookies and token change here
1

If I were going to do this, the first step is open Chrome(or FF if you like) and send a request

Press F12 enter image description here

Click that specific request, here I just refresh this page, for you, it's the login request enter image description here

And you could see what's needed, there are always cookies, and just use these cookies when you imitate the request. Sometimes only copy and pasting cookies won't work, if so, you have to make clear the meaning of every field of those cookies, and make one yourself.

Good luck.

8 Comments

Thank you... I had done it before... and there where parameters I had to post... But I don't know why there is no establishrd connection
pardon for delay... I have checked it and I sat coockies. So please see update...
@MortezaLSC I think it's OK, using requests' Session is very convenient. Is it working?
@MortezaLSC Why not show us postresp? What's the return code?
I do: print postresp ,,, The asnwer is: <Response [200]>
|

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.