0

我尝试使用 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

4

1 回答 1

1

一些注意事项:

  • 不要用 urlencode 您的帖子数据urllib,让requests处理编码。
  • requests.session默认情况下跨请求保留 cookie,因此您不必使用该cookies参数。
  • requests默认情况下遵循重定向。
  • 您不必为此站点欺骗 User-Agent。如果您想添加一些标头,您可以在session对象中执行此操作,这样您就不必headers为每个请求使用该参数。

Python代码:

import requests

url = 'https://www.rika-firenet.com/'
url_login = url+'web/login'
s = requests.session()
#s.headers['User-Agent'] = 'Mozilla/5.0'
data = {'email':'myemail@mail.com', 'password':'mypwd'}
r = s.post(url_login, data)

print(r.url)
print('Log out' in r.text)
于 2017-11-24T11:57:44.140 回答