zoukankan      html  css  js  c++  java
  • python爬虫之登录

    #-*-coding:utf-8-*-
    
    import cookielib, urllib, urllib2
    import json
    
    import threading,time
    
    class Order(object):
    ########## login
        def __init__(self):
            print "init"
    
        def speak(self):
            count = 300
            url = "http://zweb.com/login"
    
            data='{"email":"a@test.com", "password":"***"}'
            # jdata = json.dumps(data)
    
            # post_data=urllib.urlencode(jdata)
            headers ={'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 8.1)','Acc
    ept-Encoding': 'gzip, deflate','Connection':'keep-alive'}
    
            ckjar = cookielib.MozillaCookieJar('./cookie.txt2')
    
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(ckjar))
    
            req=urllib2.Request(url,data,headers)
            req.add_header('Content-Type', 'application/json')
            result = opener.open(req)
    
            htm = result.read()
            print htm
    
            ckjar.save(ignore_discard=True, ignore_expires=True)
    
            url = "http://zhweb.kingsoft.com/foodBookingOrders"
            
            data = {"foodBookingPlanId":95,"items":[{"planItemId":311},{"planItemId":309},{"
    planItemId":308}]}
    
            jdata = json.dumps(data)
    
            headers ={'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 8.1)','Acc
    ept-Encoding': 'gzip, deflate','Connection':'keep-alive'}
            while count >= 0:
                req=urllib2.Request(url,jdata)
                # req.add_header('Content-Type', 'text/plain;charset=UTF-8')
                req.add_header('Content-Type', 'application/json')
                result = opener.open(req)
    
                htm = result.read()
                print htm
    
                file_object = open('./out.txt', 'a')
                file_object.write(htm)
                file_object.close( )
    
                time.sleep(1)
                count=count - 1
    
    
    ##########
    if __name__ == "__main__":
        p = Order()
        p.speak()
    
        #count = 1
        #while count > 0:
        #    timer = threading.Timer(5, p.speak(), (p,))
        #    print "start"
        #    timer.start()
        #    timer.join()
        #    print "after join"
        #    count=count - 1
        #    timer.cancel()
  • 相关阅读:
    数据库课程设计_购书管理系统代码(sql_c#及sql_java)
    你离不开的数组
    getchar的用法
    字母大小写转化
    C语言乘除颠覆你的世界观
    循环的执行过程、适用情况和常见错误
    斐波那契数和数小方块的类型题分析方法
    C语言刷题需要注意的地方
    函数那些事
    逻辑与、或、非的使用方法
  • 原文地址:https://www.cnblogs.com/linn/p/7890366.html
Copyright © 2011-2022 走看看