urllib2模拟登陆

 

#coding=utf-8

import urllib,urllib2,cookielib
import time

def pop2():
    url_login = 'http://diy.ic.net.cn/login/'
    body = (
        ('username','icnet2'),
        ('password','xxxxxx'))
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    #opener.addheaders = [('User-agent','Opera/10.00')]
    urllib2.install_opener(opener)
    req = urllib2.Request(url_login,urllib.urlencode(body))
    u = urllib2.urlopen(req)
    print u.read()
    #继续抓你想要页面的数据
    url = 'http://diy.ic.net.cn/admins/'
    req = urllib2.Request(url)
    print urllib2.urlopen(req).read()

if __name__ == '__main__':
    #while True:
    pop2()
    #time.sleep(3) 每3秒重登陆

你可能感兴趣的:(.net,Opera)