banner
肥皂的小屋

肥皂的小屋

github
steam
bilibili
douban

Python--百度站长平台主动推送

起因#

这是一篇网站折腾介绍

soapffz已经写了40+文章了:

image

经常和别人介绍自己的网站,别人总是会问 “访问量有多少?”

soapffz只能回答:“由于没向百度推送,所以百度搜不到”

于是,这篇文章将捋一捋为什么没向百度推送以及解决方案

分为两个部分,sitemap生成和向百度推送

sitemap 生成#

前期挖的坑#

刚建设完网站时,在每篇文章写完之后链接中会存在一个index.php

为了把这个index.php去掉,变成这些链接:

https://soapffz.com/sec/247.html
https://soapffz.com/python/245.html
https://soapffz.com/tools/239.html
https://soapffz.com/sec/233.html

我在后台做了如下操作:

image

这也导致我使用一些sitemap自动生成插件的时候,会变成这样:

image

折腾#

这其中除了sitemap生成插件我也使用过在线网站,比如这个

image

可以看到花了八分半,爬了 124 条链接,点击View Sitemap Details查看详情

点击Other Downloads处所示按钮下载所有格式的文件:

image

可以看到爬取的链接都是正常的,但是问题也来了,

这样生成sitemap也太费劲了吧,后面也尝试过使用Python去爬取网站所有 url

但是想想实在没必要,就一个url格式问题而已

最终解决方案#

于是最终决定放弃在url中包含目录字段:

image

然后使用前面介绍的八云酱的插件即可生成sitemap

image

然后尝试向百度提交:

image

可以看到链接提交成功了

向百度主动推送#

sitemap处理完了还不够,我们可以使用主动推送和sitemap结合起来使得百度收录更快

百度的主动推送方式:

image

大概就是意思就是把url.txt带上自己的token传输给百度资源平台

我用Python参考网上的文章写了一个脚本

逻辑就是解析网站当前的sitemap.xml然后获取urls带上自己的token和网站名称

使用request库的post方式提交一下然后查看返回状态即可

全代码如下:

# -*- coding: utf-8 -*-
'''
@author: soapffz
@fucntion: 从网站的sitemap.xml抓取url并向百度站长平台主动推送
@time: 2019-07-25
'''

import requests
import xmltodict


class BaiduLinkSubmit(object):
    def __init__(self, site_domain, sitemap_url, baidu_token):
        self.site_domain = site_domain
        self.sitemap_url = sitemap_url
        self.baidu_token = baidu_token
        self.urls_l = []  # 存储需要爬取的url
        self.parse_sitemap()

    def parse_sitemap(self):
        # 解析网站的sitemap.xml获取所有urls
        try:
            data = xmltodict.parse(requests.get(self.sitemap_url).text)
            self.urls_l = [t["loc"] for t in data["urlset"]["url"]]
        except Exception as e:
            print("爬取sitemap.xml报错:", e)
            return
        self.push()

    def push(self):
        url = "http://data.zz.baidu.com/urls?site={}&token={}".format(
            self.site_domain, self.baidu_token)
        headers = {"Content-Type": "text/plain"}
        r = requests.post(url, headers=headers, data="\n".join(self.urls_l))
        data = r.json()
        print("成功推送{}条url到百度搜索资源平台".format(data.get("success", 0)))
        print("今天还剩余的可推送条数:", data.get('remain', 0))
        not_same_site = data.get('not_same_site', [])
        not_valid = data.get("no_valid", [])
        if len(not_same_site) > 0:
            print("由于不是本站url而未处理的url有{}条:".format(len(not_same_site)))
            for t in not_same_site:
                print(t)
        if len(not_valid) > 0:
            print("不合法的url有{}条:".format(len(not_valid)))
            for t in not_valid:
                print(t)


if __name__ == "__main__":
    site_domain = "https://soapffz.com"  # 在这里填写你的网站
    sitemap_url = "https://soapffz.com/sitemap.xml"  # 在这里填写你的sitemap.xml完整链接
    baidu_token = ""  # 填写你的百度token
    BaiduLinkSubmit(site_domain, sitemap_url, baidu_token)

我这里测试了三次,分别对应正常情况、我关掉sitemap插件和我乱写sitemap的情况

效果如下:

image

MIP&AMP 网页加速#

百度站长平台是这么介绍的:

  1. MIP (Mobile Instant Page - 移动网页加速器),是一套应用于移动网页的开放性技术标准。通过提供 MIP-HTML 规范、MIP-JS 运行环境以及 MIP-Cache 页面缓存系统,实现移动网页加速。
  2. AMP(Accelerated Mobile Pages)是谷歌的一项开放源代码计划,可在移动设备上快速加载的轻便型网页,旨在使网页在移动设备上快速加载并且看起来非常美观。百度目前可支持 AMP 提交。

反正就是针对移动端的加速,肯定对SEO有好处

这里使用的插件是Holmesian大佬的工具Typecho-AMP

插件后台界面如图所示:

image

可以看到还支持百家号,但是我这里不需要 @(滑稽),所以只要设置接口就可以了,

然后在控制台的下拉菜单中可以看到有个推送AMP/MIP到百度按钮:

image

点进去如图所示:

image

尝试推送一下:

image

发现推送不了,折腾了一会主机也没弄好,看了下主动推送规则发现非常有趣

和基础链接提交就在链接后面加了一个mip/amp,返回的参数变为success_mip/success_amp

所以把上面的代码改一下,就把推送url和推送给mip/amp的代码放在一起同时推送:

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。