前提・実現したいこと
自分のブログの内部リンクを自動で可視化するために、python3を用いているのですが、
エラーが発生しているので、解決策を教えて欲しいです。
発生している問題・エラーメッセージ
$ python3 link_network.py --url URL(自分のブログ) --savename procrasist -g Traceback (most recent call last): File "link_network.py", line 77, in <module> urls = extract_url(args.url) File "link_network.py", line 21, in extract_url html = request.urlopen(root_url+"/archive?page={}".format(page)) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error return self._call_chain(*args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: Not Found
該当のソースコード
from bs4 import BeautifulSoup import urllib from urllib import request import time import csv import os import numpy as np import time from argparse import ArgumentParser import networkx as nx import matplotlib.pyplot as plt def extract_url(root_url): page = 1 is_articles = True urls = [] entry_url = root_url + "/entry/" while is_articles: html = request.urlopen(root_url+"/page/page={}".format(page)) soup = BeautifulSoup(html, "html.parser") articles = soup.find_all("a",class_="sidelong__link") for article in articles: urls.append(article.get("href")) if len(articles) == 0: # articleがなくなったら終了 is_articles = False page += 1 return urls def make_network(root_url, urls): entry_url = root_url + "/entry/" G = nx.Graph() for url in urls: article_name= url.replace(entry_url,"").replace("/","-") G.add_node(article_name) for i,url in enumerate(urls): print(i+1,"/",len(urls)) try: html = request.urlopen(url) except urllib.error.HTTPError as e: print(e.reason) except urllib.error.URLError as e: print(e.reason) soup = BeautifulSoup(html, "html.parser") entry = soup.select(".entry-content")[0] links = entry.find_all("a") for link in links: l = link.get("href") if l in urls: linking_article_name = url.replace(entry_url,"").replace("/","-") linked_article_name = l.replace(entry_url,"").replace("/","-") print("被リンク!{} -> {}".format(linking_article_name, linked_article_name)) j = urls.index(l) G.add_edge(linking_article_name, linked_article_name) else: continue return G def visualize(G, savename, savegml): pos = nx.spring_layout(G) # グラフ形式を選択。ここではスプリングモデルでやってみる nx.draw(G, pos, with_labels=True,alpha=0.3,font_size=0.0,node_size=10) # グラフ描画。 オプションでノードのラベル付きにしている plt.savefig(savename+".png") plt.show() if savegml: nx.write_gml(G,savename+".gml") if __name__ == '__main__': parser = ArgumentParser() parser.add_argument("-u", "--url", type=str, required=True,help="input your url") parser.add_argument("-o", "--savename", type=str, default="network", help="output name") parser.add_argument('-g', "--savegml", action="store_true", default=False) args = parser.parse_args() urls = extract_url(args.url) G = make_network(args.url, urls) visualize(G, args.savename, args.savegml)
試したこと
ご指摘通り、URL+のあとが間違っておりました。
なので、ソースコードは自分のブログは"URL+/page/0"と表示されていたので、修正させて頂きました。
また、class名も自分のブログ用に変更させて頂きました。
しかし、また実行したところ、エラーは直りませんでした。
その他に変更しなければならないと思う点はentry_urlと思っています。
私のブログの場合、各記事に移動すると
ブログのURL/数値になってしまっているためこのソースコードがつかえないと思っています。
この場合、wordpressを用いて、ブログのurlを/entry/タイトル名などに変更してこのソースコードを使うのが得策だと思うのですが、
できればサイトURLを変更せずにソースコードをいじって、活用したいです。
なにか案があれば教えて頂きたいです。