티스토리는 rss 만 제공되는데 sitemap.xml 도 제공되면 좋겠네요~

무료로 사이트맵을 작성해주는 사이트가 있긴 하지만 불필요한 url 까지 생기는 문제가 있어서,

파이썬으로 한번 만들어 소스코드 공개합니다.

포스팅 주소가 숫자로 된 티스토리 블로그만 해당됩니다.

 

# coding=utf-8
from urllib2 import urlopen
from bs4 import BeautifulSoup
import xml.etree.ElementTree as xml
import time

BLOG_URL = 'https://ivps.tistory.com/' ### 블로그 주소
if ( BLOG_URL[len(BLOG_URL)-1] != '/' ):
  BLOG_URL += '/'
post_end = 0
html = urlopen(BLOG_URL)
soup = BeautifulSoup(html, "html.parser")
for hr in soup.findAll('a', {'class':'link_post'}):
  tmp = hr.get('href')[1:]
  if ( tmp != '' ):
    max = int( tmp )
    if ( post_end < max ):
      post_end = max
print "post_end " + str(post_end)

### SITEMAP XML Create
sitemap = xml.Element('urlset')
sitemap.set('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
print ('<?xml version=\'1.0\' encoding=\'utf-8\'?>')
print ('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
x1_1st = xml.SubElement(sitemap, 'url')
x2_lo = xml.SubElement(x1_1st, 'loc').text        = BLOG_URL
x2_ch = xml.SubElement(x1_1st, 'changefreq').text = 'always'
x2_pr = xml.SubElement(x1_1st, 'priority').text   = '1.0'
print ('<url><loc>' + BLOG_URL + '</loc><changefreq>always</changefreq><priority>1.0</priority></url>')
for i in range(post_end):
  url = BLOG_URL + str(i)
  try:
    html = urlopen(url)
    soup = BeautifulSoup(html, "html.parser")
    #print (soup.head.find("meta", {"name":"description"}).get('content'))
    x2_ur = xml.SubElement(sitemap, 'url')
    x3_lo = xml.SubElement(x2_ur, 'loc').text        = url
    x3_ch = xml.SubElement(x2_ur, 'changefreq').text = 'daily'
    x1_pr = xml.SubElement(x2_ur, 'priority').text   = '0.9'
    print ('<url><loc>' + url + '</loc><changefreq>daily</changefreq><priority>0.9</priority></url>')
  except:
    print (url + ' is not found')
  time.sleep(0.01)
print ('</urlset>')

#xml.dump(sitemap)
xml.ElementTree(sitemap).write('./sitemap.xml', encoding='utf-8', xml_declaration=True)

사용해 보시고 문제점이 있다면 댓글 남겨주세요~

생성된 파일을 공지사항에 첨부해놓고 해당 링크 주소를 웹마스터 도구에 제출하면 됩니다.

 

블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,