python3 爬虫小例子

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys,re
import urllib.request,urllib.parse,http.cookiejar

class myW3():
    def login(self):
        'post 数据'
        data = {"uid":self.uid,'password':self.password,'actionFlag':'loginAuthenticate'}
        
        user_agent='Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        postData = urllib.parse.urlencode(data)
        postData = postData.encode(encoding='utf_8')
        headers = {'User-Agent':user_agent}
        request = urllib.request.Request(self.url, postData,headers)
        urllib.request.urlopen(request).read().decode('utf-8')
    def cookie(self):
        '''设置cookie,非常重要'''
        cj = http.cookiejar.CookieJar()
        cp = urllib.request.HTTPCookieProcessor(cj)
        opener = urllib.request.build_opener(cp)
        urllib.request.install_opener(opener)
    def __init__(self,uid = None,password=None,url=None):
        if uid is None or password is None:
            sys.exit(1)
        self.uid = uid
        self.password = password
        self.url = url
        self.count = 0
        self.cookie()
        self.login()

posted on 2014-07-20 22:59  orientwolf  阅读(305)  评论(0编辑  收藏  举报