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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值