用Python写一个百度POST实时推送工具

2022-08-30 11:15:03 浏览数 (1)

想抄就抄吧

以下是代码

代码语言:javascript复制
import urllib,urllib2
import json,time,re
# 转载请注明出处
def GetURL(geturl,pat):#获取文章的列表
    response = urllib2.urlopen(geturl).read()
    result = re.findall(pat,response)
    return result

# Write By ITJoker
# Domain:https://www.itjoker.cn

def put(host,token,pyload):#提交内容
    url = r'http://data.zz.baidu.com/urls?site='   host   '&token=' token
    headers = {
    'Content-Type': 'text/plain',
    }
    data = pyload #urllib.urlencode(pyload)
    request = urllib2.Request(url,data,headers=headers)
    try:
        response = urllib2.urlopen(request)
        json_data = json.loads(response.read())
        print "Total:"  str(json_data['remain'])
    except urllib2.URLError,e:
        print e.reason
        
def update(host,token,pyload):#更新内容
    url = r'http://data.zz.baidu.com/update?site='   host   '&token=' token
    headers = {
    'Content-Type': 'text/plain',
    }
    data = pyload #urllib.urlencode(pyload)
    request = urllib2.Request(url,data,headers=headers)
    try:
        response = urllib2.urlopen(request)
        json_data = json.loads(response.read())
        print "Result:"  str(json_data)
    except urllib2.URLError,e:
        print e.reason

        
def delete(host,token,pyload): #删除内容
    url = r'http://data.zz.baidu.com/del?site='   host   '&token=' token
    
    headers = {
    'Content-Type': 'text/plain',
    }
    data = pyload #urllib.urlencode(pyload)
    request = urllib2.Request(url,data,headers=headers)
    try:
        response = urllib2.urlopen(request)
        json_data = json.loads(response.read())
        print "Total:"  str(json_data['remain'])
    except urllib2.URLError,e:
        print e.reason

host = "这里填写你要推送的URL"
token = "这里填写Token"
method =["http://","https://"]
domain_url = method[1] host "/atom.xml" #获取文章的目录,我这里采用获取RSS的方法来匹配
pat = "<link href=(.*?)>"

old_time = time.time()
urls = GetURL(domain_url,pat)
res =""
for i in range(len(urls)):
    res  = urls[i].replace('"','') "n"
put(host,token,res) 
new_time = time.time()
print 'used_time: ' str(int((new_time-old_time)*1000)) 'ms'

0 人点赞