凉风有信,秋月无边
亏我思娇的情绪、好比度日如年

神马搜索api推送收录python代码

文章摘要

本文介绍了一种通过Python自动化处理的方式,用于将网站的sitemap.xml文件中的URL列表生成文本文件后提交至神马搜索的站长API。具体步骤包括:使用Python脚本py_url.py从sitemap.xml文件中提取所有URL并生成sitemap.txt文本文件;然后将sitemap.txt文件通过另一个Python脚本shenma.py提交至神马搜索的站长API,以实现内容的自动收录。这种方法简化了手动操作,提高了SEO效率。

神马搜索站长自动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()
赞(0) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《神马搜索api推送收录python代码》
文章链接:https://www.1004619.com/biji/20230801131.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫