神马搜索站长自动api推送收录代码方法,本文使用python。
先将网站根目录的sitemap.xml文件生成一条条链接的txt文本,再推送提交api到神马搜索。
1.新建py_url.py,代码如下:
#coding=utf-8
import urllib
import urllib.request
import re
url='http://www.budinghua.com/sitemap.xml'
html=urllib.request.urlopen(url).read()
html=html.decode('utf-8')
r=re.compile(r'(http://www.budinghua.com.*?\.html)')
big=re.findall(r,html)
clean_xml_txt=open("x:/xxx/xxx/WWW/sitemap.txt", 'w').close()
for j, i in enumerate(big):
print(i)
if j > 1:
op_xml_txt=open('x:/xxx/xxx/WWW/sitemap.txt','a')//新建txt
op_xml_txt.write('%s\n'%i)
生成txt之后,新建shenma.py文件推送
#coding:utf8
import requests,time
def pushurls():
url = "https://data.zhanzhang.sm.cn/push?site=%s&user_name=自己神马的用户名&resource_name=mip_add&token=%s"%(domain,token) #接口调用地址 在站长平台获取
filecontents = {'file': open('x:/xxx/xxx/sitemap.txt', 'r')} #urls.txt为需要推送的URL文件,每行一个
print(filecontents)
r=requests.post(url, files=filecontents)
baiduresult =u"推送成功,结果为:%s \n" %(r.text)
print(baiduresult)
if __name__=="__main__":
domain="www.budinghua.com"
token="abc123123"//自己在神马网站的token哦
pushurls()