码迷,mamicode.com
首页 > 编程语言 > 详细

python3 爬虫小例子

时间:2014-07-21 08:22:06      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:http   os   数据   io   cti   re   

#!/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()

python3 爬虫小例子,布布扣,bubuko.com

python3 爬虫小例子

标签:http   os   数据   io   cti   re   

原文地址:http://www.cnblogs.com/orientwolf/p/3857480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!