百度/必应/神马站长三合一API推送脚本

运维干货 2年前 (2022) 导航君
13 0 0

百度/必应/神马站长三合一API推送脚本

# -*- coding: utf-8 -*-
#
#author:feko
#time:20220526
#description:这是一个集合百度、神马、必应、头条、搜狗、360、谷歌站长URL自动推送工具

# ('百度推送', 'baiduts')
# ('神马推送', 'shenmats')
# ('必应推送', 'bingts')
# ('头条推送 <-- 预留', 'toutiaots')
# ('搜狗推送 <-- 预留', 'sougouts')
# ('360推送 <-- 预留', 'sots')
# ('谷歌推送 <-- 预留', 'googlets')

import  os,sys
import requests

class ZhanZhang:
    def __init__(self,urlfile):
        self.urlfile = urlfile
        self.mysite = ""       ##填写你的网站,例如www.baidu.com
        self.baidu_token = ""  ##填写你的网站,对应百度的token
        self.baidu_num = 2500  ##百度API最高提交条数

        self.shenma_user_name = ""  ##填写你的网站,对应神马用户名
        self.shenma_token = ""  ##填写你的网站,对应神马的token
        self.shenma_num = 10000  ##神马API最高提交条数

        self.bing_token = ""  ##填写你的网站,对应必应的token
        self.bing_num = 90  ##必应API最高提交条数
        self.shenmasite = "https://%s"%(self.mysite)  ##对应http或https

    def baiduts(self):
        print("开始百度推送==========================")
        urls = self.get_urls(self.baidu_num)
        post_url = "http://data.zz.baidu.com/urls?site=%s&token=%s"%(self.mysite,self.baidu_token)
        headers = {
            'User-Agent': 'curl/7.12.1',
            'Host': 'data.zz.baidu.com',
            'Content-Type': 'text/plain',
            'Content-Length': '83'
        }
        print(urls)
        response = requests.post(post_url, headers=headers, data=urls)
        req = response.text
        print(req)

    def shenmats(self):
        print("开始神马推送==========================")
        urls = self.get_urls(self.shenma_num)
        post_url = "https://data.zhanzhang.sm.cn/push?site=%s&user_name=%s&resource_name=mip_add&token=%s"%(self.mysite,self.shenma_user_name,self.shenma_token)
        headers = {
            'User-Agent': 'curl/7.12.1',
            'Content-Type': 'text/plain',
        }
        print(urls)
        response = requests.post(post_url, headers=headers, data=urls)
        req = response.text
        print(req)

    def bingts(self):
        print("开始必应推送==========================")
        urls = self.get_urls(self.bing_num)
        urls =  urls.strip()
        urlList = urls.split('\n')
        post_url = 'https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=%s' % (self.bing_token)

        ##把列表均分500份,必应限制了每次最多500条
        temp_list = []
        for i in range(0, len(urlList), 500):
            temp_list.append(urlList[i:i + 500])
        print("总%s条链接" % (len(urlList)))
        i = 1
        for j in temp_list:
            print("第%s次推送" % (i))
            i = i + 1
            data = {
                "siteUrl": self.shenmasite,
                "urlList": j
            }
            r = requests.post(post_url, json=data)
            print(r.json())

    #获取url条数,按倒序获取
    def get_urls(self,num):
        f =open(self.urlfile,'r',encoding='utf-8')
        lines = f.readlines()  ##读取全部文件内容
        new_lines = [i for i in lines if i != "" and i != "\n" and len(i) != 0] ##去除无用的行
        new_lines = new_lines[-num:]  #获取最后几行
        new_lines_str = ''.join(new_lines).strip('\n') #转成str
        print("获取的是文件最后%s条"%(len(new_lines)))
        return new_lines_str

# 获取sitemap.xml转成sitemap.txt
def get_urls_txt(sitemap_xml):
    sitemap_xml = requests.get(sitemap_xml).text
    sitemap_xml= sitemap_xml.replace('</url>','\n')
    url = re.findall(r'<loc>(.*)</loc>', sitemap_xml)
    url_line = '\n'.join(str(url_one) for url_one in url)
    open(r'urls.txt', 'w', encoding='utf-8').write(url_line)

if __name__ == '__main__':
    sitemap_xml="xxxx/sitemap.xml"#填写你的xml地图地址
    get_urls_txt(sitemap_xml)
    urlfile = 'urls.txt'
    if not os.path.exists(urlfile):
        print("%s 不存在,exit"%(urlfile))
    res = input("请确认urls.txt文件内容是否最新[y/n]:")
    if res != 'y':
        print("脚本退出,exit")
        sys.exit(2)
    ret = ZhanZhang(urlfile)
    ret.baiduts()
    ret.shenmats()
    ret.bingts()

食用方法

  • 当前urls连接文件需要命名为urls.txt,而且每一行一条url
  • 通过填写百度,神马,必应对应得token和账号等等信息
  • 还有限制每次最多提交得条数
  • 只需要填写这部分信息即可
    百度/必应/神马站长三合一API推送脚本
  • 用python3 直接执行脚本即可
版权声明:导航君 发表于 2022年5月26日 下午6:23。
转载请注明:百度/必应/神马站长三合一API推送脚本 | 第八网址导航

相关文章

暂无评论

暂无评论...