【Python】post和get实例,有源码

最近在弄python爬虫,写一个post和get的实例,方便以后查阅。


#对接后台数据api
import requests
import json
import time

class pusher:
    def __init__(self):
        self.path="http://112.74.68.252:9800/mcp/api/content_inject"
        self.headers  = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTHL, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
        }
        self.log_on_off = True
        self.err_count = 0

    #写文件
    def writer(self, filename, txt, argu=''):
        write_flag = True
        log_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
        with open(filename, 'a', encoding='utf-8') as f:
            f.writelines(log_time)
            f.write(':'+txt)
            f.write('('+argu + ')\n')

    def loger(self, level, txt, argu = ''):
        if(self.log_on_off):
            if((level == 'e')|(level == 'E')):
                self.writer("error_log.txt",txt, argu)
                print("this is error: ---> %s %s" %(txt, argu))
            else:
                print(txt, argu)
        else:
            return

    #可选 userPicUrl、userAuthInfo、description
    def get_url(self,userId,userName,userPicUrl,userAuthInfo,contentId,title,category, \
                            videoUrl,coverUrl,duration,keyWord,description,createTime,source):
        url = 'http://112.74.68.252:9800/mcp/api/content_inject?' \
               '&userId={0}' \
               '&userName={1}' \
               '&userPicUrl={2}' \
               '&userAuthInfo={3}' \
               '&contentId={4}' \
               '&title={5}' \
               '&category={6}' \
               '&videoUrl={7}' \
               '&coverUrl={8}' \
               '&duration={9}' \
               '&keyWord={10}' \
               '&description={11}' \
               '&createTime={12}' \
               '&source={13}'.format(userId,userName,userPicUrl,userAuthInfo,contentId,title,category,videoUrl,coverUrl,duration,keyWord,description,createTime,source)
        print(url)
        return url
    
    def get_item(self, url):
        headers  = {
            'user-agent':"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Mobile Safari/537.36"
        }
        resp = requests.get(url, headers  = headers , cookies = '')
        wbdata = resp.content
        wbdata2 = json.loads(wbdata.decode('utf-8'))
        
        if wbdata2['ret'] != '0':
            print('this error: ',wbdata2['retInfo'])
            return -1
        else:
            #print('push successful')
            return 0

    def post_data(self,userId,userName,userPicUrl,userAuthInfo,contentId,title,category, \
                            videoUrl,coverUrl,duration,keyWord,description,createTime,source):
        postdata = {'userId':userId,'userName':userName,'userPicUrl':userPicUrl,'userAuthInfo':userAuthInfo,'contentId':contentId, \
            'title':title,'category':category,'videoUrl':videoUrl,'coverUrl':coverUrl,'duration':duration,'keyWord':keyWord, \
            'description':description,'createTime':createTime,'source':source}
        #print(postdata)
        return postdata

    def post_item(self, postdata):
        try:
            resp=requests.post(self.path, data=postdata)
            wbdata = resp.content
            wbdata2 = json.loads(wbdata.decode('utf-8'))
            if wbdata2['ret'] != '0':
                self.loger('E', 'this error: ', wbdata2['retInfo'])
                return -1
            else:
                #print('push successful')
                return 0
        except Exception as e:
            self.loger('E', "post_item", "post error")
            return -1

    def post_requst(self, postdata):
        ret = self.post_item(postdata)
        if(ret == -1):
            self.err_count += 1
            if(self.err_count >= 3):
                self.loger('E',"post_requst", "count out")
            else:
                self.post_requst(postdata)
            return -1
        return 0

if __name__ == '__main__':
    pushing = pusher()
    #url = pushing.get_url("get",'1','2','3','4','5','6','7','test','12','9','10','11','toutiao')
    #pushing.get_item(url)
    post_data = pushing.post_data("post",'1','2','3','4','5','6','7','test','12','9','10','11','toutiao')
    ret = pushing.post_requst(post_data)
    print('ret', ret, "df")
    if(ret == -1):
        pushing2 = pusher()
        ret = pushing2.post_requst(post_data)
        print('ret2', ret, "df")

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值