问题
I read several similar topic.. I try to follow the others examples but I'm still stuck in the middle of nowhere.. I have basic skills of python programming and little knowledge about http protocol, my two goals are: -succesfull authentication to a website via requests library -fetch data from the website after the login while the session is active
This is the code:
import requests
targetws = 'https://secure.advfn.com/login/secure'
s = requests.session()
payload_data = {'login_username': 'xxx', 'login_password': 'yyy'}
response = s.post(targetws, data=payload_data)
url='https://it.advfn.com/mercati/BIT/generali-G/ordini'
result = s.get(url)
print result.content
But I always get no success with log in.. Maybe I miss some value in post data like submit action or else? Any help will be appreciated, best regards!
Here the html code from the page:
form action="https://secure.advfn.com/login/secure" id="login_form" name="login_form" method="POST" target="">
<input type="hidden" value="aHR0cDovL2l0LmFkdmZuLmNvbQ==" name="redirect_url" id="redirect_url">
<input type="hidden" value="it" name="site" id="site">
<div class="fields"><label for="login_username">Username</label>
<input type="text" tabindex="1" class="text ui-widget-content" value =""
id="login_username" name="login_username" maxlength="64">
</div>
<div class="fields"><label for="login_password">Password</label>
<input tabindex="2" type="password" class="text ui-widget-content" value="" id="login_password" name="login_password" maxlength="16">
</div>
<div class="fields">
<strong><a href="/common/account/password/request">Se ti sei dimenticato la tua password</a></strong>
<input class="button" tabindex="3" type="submit" value="Accedi" id="login_submit">
</div>
</form
回答1:
If you look at what gets posted:
You see you need the redirect_url
and site
which you can parse from the input in the source with bs4:
import requests
from bs4 import BeautifulSoup
data = {"redirect_url": "",
"site": "uk",
"login_username": "foo",
"login_password": "bar"}
with requests.Session() as s:
log = "https://secure.advfn.com/login/secure"
r = s.get("http://uk.advfn.com/")
soup = BeautifulSoup(r.content)
redirect_url = soup.select_one("#redirect_url")["value"]
site = soup.select_one("#site")["value"]
data["redirect_url"] = redirect_url
p = s.post(log, data=data)
print(p.content)
print(s.get('https://it.advfn.com/mercati/BIT/generali-G/ordini').content)
Once you do that you will be successfully logged in.
来源:https://stackoverflow.com/questions/37816565/python-authentication-with-requests-library-via-post