Форум сайта python.su
Всем привет. Хочу создать бота для браузерной онлайн игры. Суть в том что бы законектится и в цикле парсить своё хп и бить ботов. Нужно вытащить куки при первом get запросе, запихать их в файл и использовать при последующих post запросах. Собсно сам вопрос - как вытащить куки.
Для запросов используется библиотека requests, python 3.4.1.
Можно показать простой пример?
Офлайн
http://docs.python-requests.org/en/latest/user/quickstart/#cookies
>>> url = 'http://example.com/some/cookie/setting/url' >>> r = requests.get(url) >>> r.cookies['example_cookie_name'] 'example_cookie_value'
>>> url = 'http://httpbin.org/cookies' >>> cookies = dict(cookies_are='working') >>> r = requests.get(url, cookies=cookies) >>> r.text '{"cookies": {"cookies_are": "working"}}'
Офлайн
ajib6eptСпасибо, но я это читал. И из-за того что ничего толкового не получается создал пост на форуме. Вот сайт игры
http://docs.python-requests.org/en/latest/user/quickstart/#cookies
[ { "domain": ".fdworlds.net", "expirationDate": 1474141809, "hostOnly": false, "httpOnly": false, "name": "__utma", "path": "/", "secure": false, "session": false, "storeId": "0", "value": "161727595.461491843.1410962347.1410962347.1411069809.3", "id": 1 }, { "domain": ".fdworlds.net", "expirationDate": 1411071609, "hostOnly": false, "httpOnly": false, "name": "__utmb", "path": "/", "secure": false, "session": false, "storeId": "0", "value": "161727595.1.10.1411069809", "id": 2 }, { "domain": ".fdworlds.net", "hostOnly": false, "httpOnly": false, "name": "__utmc", "path": "/", "secure": false, "session": true, "storeId": "0", "value": "161727595", "id": 3 }, { "domain": ".fdworlds.net", "expirationDate": 1426837809, "hostOnly": false, "httpOnly": false, "name": "__utmz", "path": "/", "secure": false, "session": false, "storeId": "0", "value": "161727595.1410962347.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)", "id": 4 }, { "domain": ".fdworlds.net", "expirationDate": 1411071610, "hostOnly": false, "httpOnly": false, "name": "_ym_visorc_23263192", "path": "/", "secure": false, "session": false, "storeId": "0", "value": "b", "id": 5 }, { "domain": ".www.fdworlds.net", "hostOnly": false, "httpOnly": false, "name": "PHPSESSID", "path": "/", "secure": false, "session": true, "storeId": "0", "value": "fbbj4cj8pqn10asdkncbp5", "id": 6 }, { "domain": "www.fdworlds.net", "hostOnly": true, "httpOnly": false, "name": "b", "path": "/", "secure": false, "session": true, "storeId": "0", "value": "b", "id": 7 } ]
Офлайн
Покажите код, как именно пытаешься зайти
Офлайн
import requests from bs4 import * url = 'http://www.fdworlds.net' data = { 'login':'login', 'pass':'pass' } headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0', 'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language' : 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding' : 'gzip, deflate', } s = requests.Session() resp = s.post('https://www.fdworlds.net/auth.php', headers=headers, data=data) resp.encoding = 'windows-1251' soup = BeautifulSoup(resp.text) action = soup.find('form').get('action') resp = s.post(action,headers=headers,data=data)
Отредактировано User_Name (Сен. 26, 2014 14:34:35)
Офлайн
Народ помогите. Реально из за авторизации не могу с мёртвой точки сдвинутся.
Вот так на С# сделано. Всё работает.
private const string _site = "https://www.fdworlds.net/";
private const string _login = "";
private const string _passw = "";
private CookieContainer _cookie = new CookieContainer();
private string POST(string _site, string _data, string _referer)
{
HttpWebRequest postRequest = (HttpWebRequest) WebRequest.Create(_site);
{
postRequest.Method = "POST";
postRequest.Timeout = 100000;
postRequest.Host = "www.fdworlds.net";
postRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
postRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
postRequest.Referer = _referer;
postRequest.CookieContainer = _cookie;
postRequest.ContentType = "application/x-www-form-urlencoded";
byte[] sentData = Encoding.GetEncoding(1251).GetBytes(_data);
postRequest.ContentLength = sentData.Length;
Stream sendStream = postRequest.GetRequestStream();
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();
}
WebResponse resp = postRequest.GetResponse();
Stream ReceiveStream = resp.GetResponseStream();
StreamReader sr = new StreamReader(ReceiveStream, Encoding.GetEncoding(1251));
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
string Out = String.Empty;
while (count > 0)
{
String str = new String(read, 0, count);
Out += str;
count = sr.Read(read, 0, 256);
}
return Out;
}
private string GET(string _site, string _data, string _referer)
{
HttpWebRequest getRequest = (HttpWebRequest) WebRequest.Create(_site + "?" + _data);
{
getRequest.Method = "GET";
getRequest.Host = "www.fdworlds.net";
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
getRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
if (_referer != null)
getRequest.Referer = _referer;
getRequest.CookieContainer = _cookie;
}
WebResponse resp = getRequest.GetResponse();
Stream ReceiveStream = resp.GetResponseStream();
StreamReader sr = new StreamReader(ReceiveStream, Encoding.GetEncoding(1251));
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
string Out = String.Empty;
while (count > 0)
{
String str = new String(read, 0, count);
Out += str;
count = sr.Read(read, 0, 256);
}
return Out;
}
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
txb_text.Text = POST(_site + "enter.php", "login=" + _login + "&pass=" + _passw, "https://www.fdworlds.net/auth.php");
txb_text.Text = GET(_site + "inv_user.php", " ", "http://www.fdworlds.net/inv_user.php");
}
}
}
Отредактировано User_Name (Сен. 27, 2014 01:32:25)
Офлайн