[实例]用Python登录百度空间并在别人的空间留下脚印
程序代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- import urllib2 import httplib import cookielib import urllib class BaiduBlog: def __init__(self): cookie = cookielib.CookieJar() self.httpcookie = urllib2.HTTPCookieProcessor(cookie) def login(self,username,password): url='http://passport.baidu.com/?login' postdata= (("username",username),("password",password),("Submit"," 登录 ")) self.opener = urllib2.build_opener(self.httpcookie) self.opener.addheaders=[('User-agen','MSIE/6.0')] urllib2.install_opener(self.opener) request = urllib2.Request(url,urllib.urlencode(postdata)) urllib2.urlopen(request) def visit(self,url): f = self.opener.open(url) bloghtml = f.read() f.close() if __name__=='__main__': username = 'bccn' password = 'lovebccn' url = 'http://hi.baidu.com/girlvia' bb = BaiduBlog() bb.login(username,password) bb.visit(url);