![]() |
Photo by Kat Juska on Unsplash |
Table of Contents [Disable]
Blogger のサイトマップがある場所
以前調べたので、Blogger のサイトマップがどこにあるのかはわかっている。
Blogger のサイトマップを Google Search Console に送る方法https://rteak-blog.blogspot.com/sitemap.xml
sitemap.xml
の中を見ると次のようになっている。
(元々改行やインデントはなく、後で編集したもの。)
page=1 と page=2
がある。
今後投稿記事が増えたら、これらも増えていく。( 150 記事ずつ?)
それぞれの中に、投稿記事の URL(<loc>
~ </loc> で囲まれている)
と最終更新日(<lastmod> ~ </lastmod> で囲まれている)が記載されている。
(下図は page=2 の方。これも改行やインデントはなく、後で編集したもの。)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?> | |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | |
<sitemap> | |
<loc>https://rteak-blog.blogspot.com/sitemap.xml?page=1</loc> | |
</sitemap> | |
<sitemap> | |
<loc>https://rteak-blog.blogspot.com/sitemap.xml?page=2</loc> | |
</sitemap> | |
</sitemapindex> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?> | |
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_14.html</loc> | |
<lastmod>2020-05-16T15:44:48Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_13.html</loc> | |
<lastmod>2020-05-16T15:49:37Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_12.html</loc> | |
<lastmod>2020-05-17T00:31:26Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_11.html</loc> | |
<lastmod>2020-05-17T00:37:44Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_10.html</loc> | |
<lastmod>2020-05-17T01:07:17Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_9.html</loc> | |
<lastmod>2020-05-20T14:12:39Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_75.html</loc> | |
<lastmod>2020-05-20T14:19:35Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_7.html</loc> | |
<lastmod>2020-05-08T13:49:27Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_6.html</loc> | |
<lastmod>2020-05-20T14:22:25Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_5.html</loc> | |
<lastmod>2020-05-22T13:27:54Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_4.html</loc> | |
<lastmod>2020-05-22T13:43:22Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post_3.html</loc> | |
<lastmod>2020-05-22T13:55:44Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/5.html</loc> | |
<lastmod>2020-05-22T14:12:07Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/07/blog-post.html</loc> | |
<lastmod>2020-05-22T14:21:58Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/06/blog-post_30.html</loc> | |
<lastmod>2020-05-22T14:28:59Z</lastmod> | |
</url> | |
<url> | |
<loc>https://rteak-blog.blogspot.com/2019/06/blog-post.html</loc> | |
<lastmod>2020-07-29T14:15:39Z</lastmod> | |
</url> | |
</urlset> |
投稿記事の URL と最終更新日を抜き出す方法
投稿記事の URL と最終更新日時を抜き出すには、ライブラリ requests と re を使用する。
また、各投稿記事のタイトルを抜き出すには、ライブラリ
BeautifulSoup を使用する。
手順は次のとおり。
- サイトマップを取得する(34 行目~ 38 行目参照)
requests で https://rteak-blog.blogspot.com/sitemap.xml の中身をテキスト化し、re で <loc> ~ </loc> で囲まれている部分(サイトマップの URL )をリスト( sitemap )にセットする。
今回の場合は、 https://rteak-blog.blogspot.com/sitemap.xml?page=1 と https://rteak-blog.blogspot.com/sitemap.xml?page=2 をリスト( sitemap )にセットする。 - サイトマップの数だけ処理を繰り返す( 41 行目参照)
リスト( sitemap )の要素数分繰り返す。
今回はサイトマップが 2 つあるので、2 回繰り返す。 - 投稿記事の URL と最終更新日を抜き出す( 43 行目~ 49 行目参照)
requests で各サイトマップの中身をテキスト化し、re で <loc> ~ </loc> で囲まれている部分(投稿記事の URL )をリスト( loc )にセットする。
また、<lastmod> ~ T で囲まれている部分(最終更新日)をリスト( lastmod )にセットする。
~ </lastmod> としなかったのは、時刻は不要だったため。
47 行目で <url> の前に改行コードを入れているのは、改行コードで文字列を区切ることにより、findall で文字列をうまく抜き出すため。 - 投稿記事のタイトルを抜き出す( 51行目~ 55行目参照)
各サイトマップの URL と最終更新日をすべて抜き出してから投稿記事のタイトルを抜き出す。
requests で投稿記事の URL の中身をテキスト化し、BeautifulSoup で title タグに記載されているタイトルを抜き出して、リスト( title )にセットする。 - サイトマップを HTML で編集する( 5行目~ 24行目参照)
投稿記事の URL 、タイトル、最終更新日をテーブル表示できるよう HTML で編集する。
今後の課題
そもそも次のことがやりたくて、この課題に取り組み始めた。
- 出力した HTML を自動でサイトマップのページに表示させたい。
- 各記事を自然言語処理でテキスト分類し、関連記事を自動で表示できるようにしたい。
早々に解決したいと思っている。
ソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
from bs4 import BeautifulSoup | |
def put_html(loc, title, lastmod): | |
# サイトマップを編集する | |
sitemap_html = '<table border="1">\n' | |
sitemap_html += ' <tr>\n' | |
sitemap_html += ' <th width="80%">タイトル</th>\n' | |
sitemap_html += ' <th>最終更新日</th>\n' | |
sitemap_html += ' </tr>\n' | |
for row in range(len(loc)): | |
sitemap_html += ' <tr>\n' | |
sitemap_html += ' <td><a href="' + loc[row] + '">' + title[row] + '</a></td>\n' | |
sitemap_html += ' <td>' + lastmod[row] + '</td>\n' | |
sitemap_html += ' </tr>\n' | |
sitemap_html += '</table>' | |
# ファイル書き出し | |
file = open('sitemap.html', mode='w') | |
file.write(sitemap_html) | |
file.close() | |
# sitemap を取得する | |
def get_sitemap(): | |
sitemap = [] | |
loc = [] | |
lastmod = [] | |
title = [] | |
# sitemap を取得する | |
url = requests.get('https://rteak-blog.blogspot.com/sitemap.xml') | |
text = url.text | |
text = text.replace('<sitemap>','\n<sitemap>') | |
sitemap = re.findall('<loc>(.*)</loc>', text) | |
# ページがなくなるまで繰り返す | |
for sitemap_page in range(len(sitemap)): | |
# sitemap の読み込み | |
url = requests.get(sitemap[sitemap_page]) | |
text = url.text | |
# 各記事の url と 更新日時の抽出 | |
text = text.replace('<url>','\n<url>') | |
loc += re.findall('<loc>(.*)</loc>', text) | |
lastmod += re.findall('<lastmod>(.*)T', text) | |
for page in range(len(loc)): | |
# ページ毎に読み込み、タイトルを取得する | |
html = requests.get(loc[page]) | |
soup = BeautifulSoup(html.content, 'html.parser') | |
title += soup.find('title') | |
put_html(loc, title, lastmod) | |
get_sitemap() | |