Last active
April 20, 2022 14:50
-
-
Save x5gtrn/6e3fdfb5b22ed4a8d1b82f6fe4329127 to your computer and use it in GitHub Desktop.
As a shell script, this script collects and shows a permalink of all entries from the target Ameba Blog Site.
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 sys | |
import requests | |
from bs4 import BeautifulSoup | |
BLOG_TOP_URL = "https://ameblo.jp" | |
BLOG_ID = "" | |
REVERSED = True | |
args = sys.argv | |
if len(args) >= 2 and (isinstance(args[1], str) and len(args[1]) > 0): | |
BLOG_ID = args[1] | |
if len(args) == 3 and args[2] == "--from-latest": | |
REVERSED = False | |
else: | |
print("Specify Blog ID as a parameter.") | |
sys.exit(1) | |
def do(): | |
print("Target: {0}/{1}".format(BLOG_TOP_URL, BLOG_ID)) | |
entrylist_url = "{0}/{1}/entrylist.html".format(BLOG_TOP_URL, BLOG_ID) | |
entrylist_list = get_entrylist_list(entrylist_url) | |
entry_list = get_entry_list(entrylist_list) | |
all_entry_page_list = get_all_page_list(entry_list) | |
print_all_entry_url(all_entry_page_list) | |
def get_entrylist_list(html): | |
entrylist_list = [html] | |
page_no = 1 | |
while True: | |
html = requests.get(html).content | |
soup = BeautifulSoup(html, "lxml") | |
next_page = soup.find("a", {"class", "skin-paginationNext skin-btnIndex js-paginationNext"}) | |
if isinstance(next_page, type(None)): | |
if REVERSED: | |
entrylist_list.reverse() | |
return entrylist_list | |
else: | |
page_no = page_no + 1 | |
next_page.__delitem__("href") | |
next_page.__setitem__("href", "{0}/{1}/entrylist-{2}.html".format(BLOG_TOP_URL, BLOG_ID, str(page_no))) | |
entrylist_list.append(next_page["href"]) | |
html = next_page["href"] | |
def get_entry_list(all_entrylist_list): | |
entry_list = [] | |
duplicate_count = 0 | |
not_entry_url_count = 0 | |
for html in all_entrylist_list: | |
html = requests.get(html).content | |
soup = BeautifulSoup(html, "lxml") | |
a_list = soup.find_all("a", {"class", ""}) | |
for a in a_list: | |
h = a.get("href") | |
if h.startswith("{0}/entry-".format(BLOG_ID)): | |
if a_list.count(a) > 0: | |
a_list.remove(a) | |
duplicate_count = duplicate_count + 1 | |
else: | |
a_list.remove(a) | |
not_entry_url_count = not_entry_url_count + 1 | |
entry_list.append(a_list) | |
print(str(duplicate_count) + " of duplicated URLs were removed from the source.") | |
print(str(not_entry_url_count) + " of not entry URLs were removed from the source") | |
print("---") | |
if REVERSED: | |
entry_list.reverse() | |
return entry_list | |
def get_all_page_list(all_entry_list): | |
all_page_list = [] | |
for entry_list in all_entry_list: | |
for url in entry_list: | |
title = url.string | |
h = url.get("href") | |
url.__delitem__("href") | |
url.__setitem__("href", BLOG_TOP_URL + h) | |
all_page_list.append(str(url.get("href"))) | |
if REVERSED: | |
all_page_list.reverse() | |
return all_page_list | |
def print_all_entry_url(all_pages): | |
for page in all_pages: | |
print(page) | |
print("---") | |
print("There are {0} entry pages in <{1}/{2}>".format(len(all_pages), BLOG_TOP_URL, BLOG_ID)) | |
do() |
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
$ python3 ameblo_permalink_collector.py systema-m-tg | |
Target: https://ameblo.jp/systema-m-tg | |
0 duplicated URLs were removed from the source. | |
1006 of not entry URLs were removed from the source. | |
--- | |
https://ameblo.jp/systema-m-tg/entry-11150872120.html | |
https://ameblo.jp/systema-m-tg/entry-11153406006.html | |
https://ameblo.jp/systema-m-tg/entry-11154202149.html | |
https://ameblo.jp/systema-m-tg/entry-11156211429.html | |
https://ameblo.jp/systema-m-tg/entry-11157538875.html | |
https://ameblo.jp/systema-m-tg/entry-11160316504.html | |
https://ameblo.jp/systema-m-tg/entry-11161055384.html | |
https://ameblo.jp/systema-m-tg/entry-11162373781.html | |
https://ameblo.jp/systema-m-tg/entry-11163102598.html | |
https://ameblo.jp/systema-m-tg/entry-11164849149.html | |
: | |
https://ameblo.jp/systema-m-tg/entry-12731099271.html | |
https://ameblo.jp/systema-m-tg/entry-12731533602.html | |
https://ameblo.jp/systema-m-tg/entry-12732943831.html | |
https://ameblo.jp/systema-m-tg/entry-12733319961.html | |
https://ameblo.jp/systema-m-tg/entry-12733980012.html | |
https://ameblo.jp/systema-m-tg/entry-12734622246.html | |
https://ameblo.jp/systema-m-tg/entry-12735445696.html | |
https://ameblo.jp/systema-m-tg/entry-12735925325.html | |
https://ameblo.jp/systema-m-tg/entry-12737079742.html | |
https://ameblo.jp/systema-m-tg/entry-12738532230.html | |
--- | |
There are 958 entry pages in <https://ameblo.jp/systema-m-tg> | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment