snipt

Ctrl+h for KB shortcuts
#8760

Python

logowanie do naszej klasy

from mechanize import Browser
import re
import urllib

br = Browser()

user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; pl; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'
br.set_handle_robots(False)
url = 'http://nasza-klasa.pl'
cookie = ''

br.addheaders = [('User-agent', user_agent), ('Cookie', cookie)]
br.open(url)

forms = [form for form in br.forms()]
br.form = forms[0]
br['login'] = 'xxxxxxxxxx'
br['password'] = 'xxxxxxxxxxx'

resp = br.submit()
content = resp.read()
https://snipt.net/embed/45a09036a3cd7eb6e5bf88cce72ce0b4/
https://snipt.net/raw/45a09036a3cd7eb6e5bf88cce72ce0b4/
45a09036a3cd7eb6e5bf88cce72ce0b4
python
Python
21
2014-04-18T16:56:54
True
False
/api/public/snipt/8760/
logowanie-do-naszej-klasy
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a> <a href="#L-19">19</a> <a href="#L-20">20</a> <a href="#L-21">21</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kn">from</span> <span class="nn">mechanize</span> <span class="kn">import</span> <span class="n">Browser</span> </span><span id="L-2"><a name="L-2"></a><span class="kn">import</span> <span class="nn">re</span> </span><span id="L-3"><a name="L-3"></a><span class="kn">import</span> <span class="nn">urllib</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="n">br</span> <span class="o">=</span> <span class="n">Browser</span><span class="p">()</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="n">user_agent</span> <span class="o">=</span> <span class="s">&#39;Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; pl; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3&#39;</span> </span><span id="L-8"><a name="L-8"></a><span class="n">br</span><span class="o">.</span><span class="n">set_handle_robots</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span> </span><span id="L-9"><a name="L-9"></a><span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://nasza-klasa.pl&#39;</span> </span><span id="L-10"><a name="L-10"></a><span class="n">cookie</span> <span class="o">=</span> <span class="s">&#39;&#39;</span> </span><span id="L-11"><a name="L-11"></a> </span><span id="L-12"><a name="L-12"></a><span class="n">br</span><span class="o">.</span><span class="n">addheaders</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;User-agent&#39;</span><span class="p">,</span> <span class="n">user_agent</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;Cookie&#39;</span><span class="p">,</span> <span class="n">cookie</span><span class="p">)]</span> </span><span id="L-13"><a name="L-13"></a><span class="n">br</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">url</span><span class="p">)</span> </span><span id="L-14"><a name="L-14"></a> </span><span id="L-15"><a name="L-15"></a><span class="n">forms</span> <span class="o">=</span> <span class="p">[</span><span class="n">form</span> <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">br</span><span class="o">.</span><span class="n">forms</span><span class="p">()]</span> </span><span id="L-16"><a name="L-16"></a><span class="n">br</span><span class="o">.</span><span class="n">form</span> <span class="o">=</span> <span class="n">forms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> </span><span id="L-17"><a name="L-17"></a><span class="n">br</span><span class="p">[</span><span class="s">&#39;login&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;xxxxxxxxxx&#39;</span> </span><span id="L-18"><a name="L-18"></a><span class="n">br</span><span class="p">[</span><span class="s">&#39;password&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;xxxxxxxxxxx&#39;</span> </span><span id="L-19"><a name="L-19"></a> </span><span id="L-20"><a name="L-20"></a><span class="n">resp</span> <span class="o">=</span> <span class="n">br</span><span class="o">.</span><span class="n">submit</span><span class="p">()</span> </span><span id="L-21"><a name="L-21"></a><span class="n">content</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> </span></pre></div> </td></tr></table>
http, login, python