前后端分離項(xiàng)目做爬蟲收錄,提供蜘蛛爬行最簡單方案,創(chuàng)建sitemap xml
2024年5月13日11:36:01
現(xiàn)在很多項(xiàng)目是vue react angular開發(fā)的,但是百度爬蟲對(duì)這樣的項(xiàng)目支持不好,很多時(shí)候回去采用一些 服務(wù)器端渲染(SSR) 和靜態(tài)站點(diǎn)生成(SSG) ,當(dāng)然有些框架支持ssr和ssg效果不好,還有些想不不破壞項(xiàng)目自身的提前下的方案呢?
參考:https://blog.csdn.net/andy_68147772/article/details/135118183
很多年前接手一個(gè)angularjs的項(xiàng)目,但是要搞爬蟲收錄,搞了好久那個(gè)時(shí)候ssr支持的不太好,所以想了另一個(gè)方法。
爬蟲是會(huì)先爬取 http://127.0.0.1/robots.txt 去讀取爬蟲規(guī)則的,這里面是可以指定stemap xml地址的
例如:
User-agent: *
Allow: /
Sitemap: https://www.xxx.cn/express.xml
Sitemap: https://www.xxx.cn/lastest.xml
Sitemap: https://www.xxx.cn/sitemap1.xml
Sitemap: https://www.xxx.cn/sitemap2.xml
Sitemap: https://www.xxx.cn/sitemap3.xml
Sitemap: https://www.xxx.cn/sitemap4.xml
sitemap規(guī)范
https://www.sitemaps.org/protocol.html#index
如果有sitemap的地址,爬蟲就會(huì)去讀取sitemap
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.xxx.com/page1</loc>
<lastmod>2023-01-01</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://www.xxx.com/page2</loc>
<lastmod>2023-01-02</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<!-- more URLs... -->
</urlset>
那么就可以直接在get的頁面的內(nèi)容接口直接放在地圖,讓蜘蛛直接獲取接口返回的json內(nèi)容,就不需要渲染,達(dá)到讓不使用ssr,ssg來讓爬蟲爬取內(nèi)容的方法。
laravel支持的sitemap生成工具
composer require spatie/laravel-sitemap
如果內(nèi)容過多,建議分拆xml文件 Sitemap: https://www.xxx.cn/sitemap4.xml 文件,不然一次太大影響爬蟲獲取接口返回的時(shí)候,注意http頭時(shí)json格式。
會(huì)去求證一下百度蜘蛛爬蟲是否可行,我們當(dāng)年是可以的,因?yàn)榕老x技術(shù)一直在變,最好找官方支持一下。
QQ二群 166427999
如果項(xiàng)目有技術(shù)瓶頸問題,請(qǐng)聯(lián)系↓↓
QQ: 903464207
微信: zx903464207
posted on 2024-05-14 13:50 zh7314 閱讀(175) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)