java實現百度站長主動推送個人網站鏈接

個人博客已經更新歡迎來訪:www.stopping.top

一、百度站長鏈接提交

  1. 鏈接提交工具是網站主動向百度搜索推送數據的工具,本工具可縮短爬蟲發現網站鏈接時間,網站時效性內容建議使用鏈接提交工具,實時向搜索推送數據。本工具可加快爬蟲抓取速度,無法解決網站內容是否收錄問題
  2. 百度搜索資源平臺爲站長提供鏈接提交通道,您可以提交想被百度收錄的鏈接,百度搜索引擎會按照標準處理,但不保證一定能夠收錄您提交的鏈接。

二、提交方式

  1. 主動推送:最爲快速的提交方式,建議您將站點當天新產出鏈接立即通過此方式推送給百度,以保證新鏈接可以及時被百度收錄。使用主動推送功能效果
    (1)及時發現:可以縮短百度爬蟲發現您站點新鏈接的時間,使新發布的頁面可以在第一時間被百度收錄
    (2)保護原創:對於網站的最新原創內容,使用主動推送功能可以快速通知到百度,使內容可以在轉發之前被百度發現
  2. sitemap:您可以定期將網站鏈接放到Sitemap中,然後將Sitemap提交給百度。百度會週期性的抓取檢查您提交的Sitemap,對其中的鏈接進行處理,但收錄速度慢於主動推送。
  3. 手工提交:如果您不想通過程序提交,那麼可以採用此種方式,手動將鏈接提交給百度。
  4. 自動推送:是輕量級鏈接提交組件,將自動推送的JS代碼放置在站點每一個頁面源代碼中,當頁面被訪問時,頁面鏈接會自動推送給百度,有利於新頁面更快被百度發現。

三、主動推送-Java實現

  1. 推送方式、推送接口、接口返回值看官網 文檔地址。本文實現主動推送的邏輯是在管理員每次發佈博客成功時將生成的博客鏈接推送到百度。

  2. 使用Post推送
    ###(1) 新建工具類PostUrl.java

/**
 * @Author: stopping
 * @Date: 2020/04/30 18:30
 * 轉載註明出處、個人博客網站:www.stopping.top
 */
@Service
public class PostUrl {

    private final Logger logger = LoggerFactory.getLogger(PostUrl.class);

    private RestTemplate restTemplate;

    public void BaiduPost(String sitePath,String url){
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            //建立連接
            URLConnection connection = new URL(sitePath).openConnection();
            //設置請求屬性
            connection.setRequestProperty("Host","data.zz.baidu.com ");
            connection.setRequestProperty("User-Agent","curl/7.12.1 ");
            connection.setRequestProperty("Content-Length","83");
            connection.setRequestProperty("Content-Type", "text/plain");

            connection.setDoInput(true);
            connection.setDoOutput(true);
            //獲取connection輸出流
            out = new PrintWriter(connection.getOutputStream());
            out.print(url.trim());
            out.flush();

            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            String result = "";
            while ((line = in.readLine())!=null){
                result += line;
            }
            logger.info("post提交結果:{}",result);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            logger.info("發送POST異常");
            e.printStackTrace();
        }
		finally{
			in.close();
		}
    }
}

(2)在測試用例測試

```java
public class PostUrlTest extends TestCase {
	@Test
	public void testBaidu(){
		String siteurl = "http://data.zz.baidu.com/urls?site=your site&token=xxx";
		String blogurl = "http://www.stopping.top/u/Blog/19";
		PostUrl postUrl = new PostUrl();
		postUrl.BaiduPost(siteurl,blogurl);
	}
}

```

(3)測試結果

	21:04:17.492 [http-nio-8080-exec-2] INFO  c.x.b.b.c.UserspaceController - 百度接口http://www.stopping.top/u/Blog/1
	21:04:17.736 [http-nio-8080-exec-2] INFO  c.x.blog.blogprototype.util.PostUrl - post提交結果:{"remain":2997,"success":1}

(4)說明

根據官方文檔推送反饋,測試結果反饋的結果是正確並已經推送到百度。
在這裏插入圖片描述

###(5)使用主動推送之後的數據變化
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章