Created
November 15, 2011 23:46
-
-
Save ksamuel/1368779 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # vim: ai ts=4 sts=4 et sw=4 | |
| import urllib | |
| import urllib2 | |
| import cookielib | |
| import os | |
| import gzip | |
| import io | |
| import mechanize | |
| theurl = 'http://www.redlist-ultimate.be' | |
| # an example url that sets a cookie, | |
| # try different urls here and see the cookie collection you can make ! | |
| # Browser | |
| br = mechanize.Browser() | |
| # Cookie Jar | |
| cj = cookielib.LWPCookieJar() | |
| br.set_cookiejar(cj) | |
| # Browser options | |
| br.set_handle_equiv(True) | |
| br.set_handle_gzip(True) | |
| br.set_handle_redirect(True) | |
| br.set_handle_referer(True) | |
| br.set_handle_robots(False) | |
| # Follows refresh 0 but not hangs on refresh > 0 | |
| br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) | |
| headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
| 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3', | |
| 'Accept-Encoding':'gzip,deflate,sdch', | |
| 'Accept-Language':'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', | |
| 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', | |
| 'Connection':'keep-alive', | |
| 'Host':'www.redlist-ultimate.be', | |
| 'Origin':'http://www.redlist-ultimate.be', | |
| 'Content-Length':'81', | |
| 'X-Requested-With': 'XMLHttpRequest', | |
| 'Cache-Control':'no-cache', | |
| 'Referer':'http://www.redlist-ultimate.be/index.php?lien=liens&type=films&id=26631&im=Les_sorcieres_d_Oz', | |
| 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/11.10 Chromium/14.0.835.202 Chrome/14.0.835.202 Safari/535.1' | |
| } | |
| br.addheaders = headers.items() | |
| req = urllib2.Request(theurl) | |
| # create a request object | |
| print 1 | |
| handle = br.open(req) | |
| handle.read() | |
| url = "http://www.redlist-ultimate.be/ajax/getliens.php" | |
| print 2 | |
| req = urllib2.Request(url, | |
| urllib.urlencode({'restriction':'normal', 'type':'films', | |
| 'id':'26631', 'nom': 'Les sorcières d\'Oz', | |
| 'surveille': 0, 'genre':''})) | |
| response = br.open(req) | |
| print response.read() | |
| # URL1 = "http://www.redlist-ultimate.be/" | |
| # print 'GET: %s' % URL1 | |
| # request = mechanize.Request(URL1, timeout=120) | |
| # for key, value in headers.items(): | |
| # request.add_header(key, value) | |
| # response = mechanize.urlopen(request) | |
| # # cj = mechanize.CookieJar() | |
| # # cj.extract_cookies(response, request) | |
| # URL2 = "http://www.redlist-ultimate.be/ajax/getliens.php" | |
| # # let's say this next request requires a cookie that was set in response | |
| # params = "?type=films&id=24611&restriction=normal&surveille=0&nom=The river murders" | |
| # request2 = mechanize.Request(URL2, params, timeout=120) | |
| # headers.update({ | |
| # 'Connection':'keep-alive', | |
| # 'Content-Type':'application/x-www-form-urlencoded', | |
| # }) | |
| # for key, value in headers.items(): | |
| # request2.add_header(key, value) | |
| # # request2.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7") | |
| # # request2.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') | |
| # # request2.add_header('X-Requested-With', 'XMLHttpRequest') | |
| # # cj.add_cookie_header(request2) | |
| # response2 = mechanize.urlopen(request2) | |
| # print 'POST: %s' % URL2 | |
| # print response2.read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment