How to login using requests in Python?

Viewed 3818

I would like to get data from https://creis.fang.com/.

However, I need to login the page first.

There are 4 values I need to fill in.

I tried to use requests but failed.

Here is my code:

import requests
url = 'https://creis.fang.com/'
s = requests.Session()
data ={'cnname': 'myname', 'cnotp':'abc', 'cntempcode':'123', 'cnproducttitle':'企业版'}
r = s.post(url=url, data=data)

Can you help me?

Thanks.

enter image description here

enter image description here enter image description here enter image description here

2 Answers

First of all, the login request and the code you have is different.

The password that is sent is encrypted using RSA. I am not sure if it changed on every request or the logic to encrypt is fixed. So you need to make sure the password that is sent is encrypted

import requests

url = 'https://creis.fang.com/'
s = requests.Session()
s.get(url)

data = {
    "sloginID": "test",
    "sPassword": "8dfddc7151749b91cedc965dec30295f34f5ab3f46679d3fa8c8d917474d162c63adcc6c9c0ea42f4cfc3fdf06bf98e4a9225f6fb8690874a028f17f2f01726259b4a32fc49e3e287a7832b6799916531603d06763b8309ca144f8c6828a9273bd4612d6b759193ee69c3608e3072f914e9af05ab2ea4bf1ab12816995f51711",
    "sHID":"",
    "sTempCode":"",
    "product": "sfangdata", 
    "rsakey":"",
    "l": "cn"
}

res = s.post("https://creis.fang.com/login/login/?r=0.6500200817926693", data=data)

print (res.json())

After that, I get the error

{'code': 'error', 'msg': '登录器权限已过期!', 'product': 'sfangdata', 'other': 'login'}

Which is because I don't have the correct credentials

In this case, the password is encrypted in javascript before the request is sent by the browser.

What I'd recommend at this point is to use something like https://selenium-python.readthedocs.io/ instead of requests.

That way you'll avoid having to reverse-engineer the javascript portion of things and be able login easily.

If you then need to proceed with requests, you could attempt to retrieve the cookies generated by selenium and continue with requests, but I'd recommend to use selenium for everything instead.

Related