推送自己网站到必应搜索引擎 PHP+Python

推送自己网站链接到必应的搜索引擎!

python的代码

import requests
import re

#根据站点地图获取链接
def get_urls(sitemap):
    response = requests.get(url=sitemap)
    urls = re.findall('<loc>(.*?)</loc>', response.text)
    return urls


#提交到必应
def submit_bing(site_url: str, url_list: list, api_key: str):
    try:
        response = requests.post(url=f"https://ssl.bing.com//webmaster/api.svc/json/SubmitUrlbatch?apikey={api_key}",
                                 json={
                                     'siteUrl': site_url,
                                     'urlList': url_list
                                 },
                                 headers={
                                     'Host': 'ssl.bing.com',
                                     'Content-Type': 'application/json; charset=utf-8',
                                 })
        print(response.status_code)
    except Exception as e:
        print('e')
    finally:
        if response.status_code == 200 and response.json().get('d') is None:
            print('推送成功:')
            for i in url_list:
                print(i)
        else:
            print('推送失败:推送配额不足,https://www.bing.com/webmasters/submiturl')


if __name__ == '__main__':
    apiKey = ''
    siteUrl = 'https://www.pbba.cn/'
    urlList = get_urls('https://www.pbba.cn/sitemap_cate_1.xml')
    submit_bing(site_url=siteUrl, url_list=urlList, api_key=apiKey)

PHP的代码

<?php

// 根据站点地图获取链接
function get_urls($sitemap) {
    $response = file_get_contents($sitemap);
    preg_match_all('/<loc>(.*?)<\/loc>/', $response, $matches);
    return $matches[1];
}

// 提交到必应
function submit_bing($site_url, $url_list, $api_key) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://ssl.bing.com//webmaster/api.svc/json/SubmitUrlbatch?apikey={$api_key}");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
        'siteUrl' => $site_url,
        'urlList' => $url_list
    ]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Host: ssl.bing.com',
        'Content-Type: application/json; charset=utf-8'
    ]);

    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($http_code == 200 && json_decode($response)->d == null) {
        echo "推送成功:\n";
        foreach ($url_list as $url) {
            echo $url . "\n";
        }
    } else {
        echo "推送失败:推送配额不足,https://www.bing.com/webmasters/submiturl\n";
    }
}

$apiKey = '';
$siteUrl = 'https://www.pbba.cn/';
$urlList = get_urls('https://www.pbba.cn/sitemap_cate_1.xml');
submit_bing($siteUrl, $urlList, $apiKey);

?>
© 版权声明
THE END
喜欢就支持一下吧
点赞16
评论 抢沙发

请登录后发表评论

    暂无评论内容