我尝试使用 python 登录这个网站(并在自动化一些操作之后):https ://www.rika-firenet.com/web/login
正如您在页面上看到的,html 代码是这样的:
<form id="login" method="POST" action="/action_page.php" data-ajax="false">
<input type="text" data-theme="b" name="email" value="" placeholder="email@example.com">
<input type="password" data-theme="b" name="password" value="" placeholder="password">
<button type="submit" data-theme="a" data-icon="fa-sign-in" data-iconpos="right">
Connect
</button>
</form>
所以我在 python 中尝试了:
import requests
import urllib.parse
url = 'https://www.rika-firenet.com/'
url_login = url+'web/login'
client = requests.session()
payload = urllib.parse.urlencode({
'email':'myemail@mail.com',
'password':'mypwd'
})
print("+-[url get] : {}".format(url_login))
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
r = client.get(url, headers=headers, allow_redirects=True)
print(r.cookies)
print("+-[url post] : {}".format(url_login))
p = client.post(url_login, data=payload, headers=headers, cookies=r.cookies)
print(p.content)
我在 get 中获得了 cookie,但我有原始页面作为 post 请求的回报。
+-[url get] : https://www.rika-firenet.com/web/login
<RequestsCookieJar[<Cookie connect.sid=s%3Auzv2S7zjhW6Hs2S7hOKyx6icXhbSSSTx.t%2Fg32GT2s2zIbvGI3kq%2Fht%2FR3BDa8aPUwTmWl%2BYktKU for www.rika-firenet.com/>]>
+-[url post] : https://www.rika-firenet.com/web/login
有人成功使用了这个 php 代码:
function login ()
{
global $login_url,$username_rika,$password_rika,$path_cookie;
$postinfo = "email=".$username_rika."&password=".$password_rika;
$status = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $path_cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
$return = curl_exec($ch);
// Retourne le numéro d'erreur de la dernière opération cURL.
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
$status['connected'] = false;
$status['curl_errno'] = curl_errno($ch);
$status['curl_error'] = curl_error($ch);
exit; // mettre en veille en mode développement
}
else {
//echo "Data received phase 1 : $return\n";
$status['connected'] = true;
}
curl_close($ch);
return $status;
}
但由于我不是 php 专家,我真的不明白它为什么会起作用。
有什么线索吗?
谢谢
格雷格
ps : 解决后,Rika炉灶控制完整代码可见https://github.com/iero/Rika-Stove