以 Python 抓取需要 Cookie 的網頁


通過下面的方法.Cookie 就可以成功保留下來.
之後就可以再通過 urllib2.urlopen 來讀取需要登錄的頁面..

1
2
3
4
5
6
7
8
9
import urllib2,cookielib,urllib

cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

urllib2.install_opener(opener)

query_string = urllib.urlencode({'username':'test','password':'xxxxxx'})
login_response = urllib2.urlopen('http://localhost/php/test/python-login.php', query_string);