URLãã³ãã¼ãã¦ã¯ãªãããã¼ãã«éãã¨ãWebã¹ã¯ã¬ã¤ãã³ã°ã§è¨äºã®ã¿ã¤ãã«ãåå¾ãã¦è¡¨ç¤ºããPythonã®GUIã¢ããªï¼url2titleï¼ãä½ãã¾ããã
èªåã®ã¯ã¦ãªããã°ã®è¨äºã®URLãæ¥ä»ã®å ´åã«ãããã¨è¦ãã¨ãã«ãä½ã®è¨äºï¼ã¨ãªããã¨ãããããã¾ãããããªã¨ãããã¡ãã¡ãã©ã¦ã¶ã§éãã¦ã¿ã¤ãã«ã確èªããã®ãæéãªã®ã§ããã¼ã«ã¨ãã¦ä½ã£ã¦ã¿ã¾ããã
æè¿ã¯ãè¨äºãæ¸ãã¨ãã«ããããããURLãè¨å®ããããã«ãã¦ãã®ã§ãããéå»ã®è¨äºã¯URLãå¤æ´ããåæ°ããªããå°ã£ã¦ããã®ã§ä½ãã¾ããï¼
ããã§ã¯ããã£ã¦ããã¾ãï¼
ã¯ããã«
ä»åä½ã£ãGUIã¢ããªã¯ãååç´¹ä»ãããTkEasyGUIãã使ã£ã¦ã¾ããååã®è¨äºã¯ãã¡ãã§ãã
daisuke20240310.hatenablog.com
ã¾ããURLããã¿ã¤ãã«ã«å¤æããæ¹æ³ã¯ã以åã«ç´¹ä»ããWebã¹ã¯ã¬ã¤ãã³ã°ã使ã£ã¦ã¾ãããã®ã¨ãã®è¨äºã¯ãã¡ãã§ãã
daisuke20240310.hatenablog.com
ãã®ä¸¡æ¹ã®å
容ãçµã¿åãããã ãã®ç°¡åGUIã¢ããªã§ãã
ãã¨ãä¸å¿ãã¹ã¯ã¬ã¤ãã³ã°ã使ã£ã¦ããã®ã§ãã©ãã§ãã¢ã¯ã»ã¹ãã¦ããããã§ã¯ãªãã®ã§ãèªåã®ãµã¤ãã«éå®ãã¦ä½¿ã£ã¦ããã ãå¶éãå
¥ãã¦ãã¾ãã
ã§ã¯ã使ãæ¹ã®èª¬æã¨ãã½ã¼ã¹ã³ã¼ãã®èª¬æããã¦ããã¾ãã
äºåæºå
ããã§ã¯ãWindowsã§ä½¿ãæ¹æ³ã説æãã¾ããGUIã¢ããªã®ååã¯ãurl2titleãã¨ãã¾ãã
Pythonãã¤ã³ã¹ãã¼ã«ãã¦ãããã¨ãåæã«ãªãã¾ãã
ä¸å¿ããã¦ã³ãã¼ãå
ã ã示ãã¦ããã¾ãã
www.python.org
Pythonã®ã©ã¤ãã©ãªãããã¤ãå¿
è¦ã«ãªãã¾ãã
ã³ãã³ãããã³ãããªã©ã§ã以ä¸ã®3ã¤ã®ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¾ãã
pip install requests beautifulsoup4 TkEasyGUI
æºåã¯ä»¥ä¸ã§ãã
url2titleã®ä½¿ãæ¹
ã¾ãã以ä¸ãããã¦ã³ãã¼ããã¾ãã
github.com
ä¸ãã¯ãªãã¯ããã¨ä»¥ä¸ã®ãããªç»é¢ã«ãªãã¨æãã®ã§ããã¦ã³ãã¼ããã¿ã³ãæ¼ãã¨ãã¦ã³ãã¼ãã§ãã¾ããä¸ã®æ¹ã«ã½ã¼ã¹ã³ã¼ããè²¼ã£ã¦ã¾ãã®ã§ããããã³ãã¼ãã¦ã¨ãã£ã¿ã«è²¼ãä»ããæ¹æ³ã§ã大ä¸å¤«ã§ãã
ãã¦ã³ãã¼ããããã¡ã¤ã«ãurl2title.pywããé©å½ãªã¨ãã£ã¿ã§éãã¾ããã½ã¼ã¹ã³ã¼ããã³ãã¼ããæ¹ã¯ããã¡ã¤ã«åã¯ä½ã§ãããã§ãããæ¡å¼µåã¯ãpywãã«ããå¿
è¦ãããã¾ãããpywãã«ãããã¨ã§ãããã«ã¯ãªãã¯ã§å®è¡ã§ããããã«ãªãã¾ãã
ããã§ã¯IDLEã®ã¨ãã£ã¿ã使ãã¾ãã
ãã¡ã¤ã«ãå³ã¯ãªãã¯ãã¦ããEdit with IDLEããã¯ãªãã¯ãã¦éãã¾ãã
8è¡ç®ãå¤æ´ããå¿
è¦ãããã¾ãããã¦ã³ãã¼ããããurl2titleãã®ç¶æ
ã ã¨ãå¤æã§ããURLããç§ã®ããã°ã®ãã¼ã¸ã ãã«éå®ããã¦ãã¾ããç§ã®ããã°ã®ããããã¼ã¸ã®URLãè¨å®ãã¦ããã®ã§ããããèªåã®ãµã¤ãã®ããããã¼ã¸ã®URLã«æ¸ãæãã¦ãã ããã
ã§ã¯ããurl2titleãããããã«ã¯ãªãã¯ãã¦èµ·åãã¾ãã
ããã¨ãå°ããªã¦ã£ã³ãã¦ã®ãurl2titleããèµ·åãã¾ãã
ãã¨ã¯ãã¿ã¤ãã«ã調ã¹ããURLãã³ãã¼ãã¦ã¯ãªãããã¼ãã«å
¥ããã¨ãã¿ã¤ãã«ã表示ãããã¨æãã¾ãã
ãã®ä¾ã§ã¯ãªã³ã¯ãå³ã¯ãªãã¯ãã¦URLãã³ãã¼ãã¦ãã¾ãããhttps://daisuke20240310.hatenablog.com/entry/2024/04/04/032847
ã¨ããæååãã³ãã¼ãã¦ãåããã¨ãåºæ¥ã¾ãã
ç°¡åã§ããã使ãæ¹ã¯ä»¥ä¸ã§ãã
ã½ã¼ã¹ã³ã¼ãã®èª¬æ
ã½ã¼ã¹ã³ã¼ãã®èª¬æããã¾ãã
ä¸å¿ããTkEasyGUI(Python)で簡単GUI(PySimpleGUI有料化に伴い移行します!) - daisukeの技術ブログãã¨ããPythonでWebスクレイピング(ChatGPTにPythonコードを生成してもらう) - daisukeの技術ブログãã§ã説æããã®ã§ãä»åã¯æµãã ãã®èª¬æã«ãããã¨æãã¾ãã
ä¸ãã8è¡ç®ã®MYURLã¨ããå¤æ°ããä¸ã§ã説æããå¤æ´ãå¿
è¦ãªã¨ããã§ãã
import TkEasyGUI as eg
import pyperclip
import requests
from bs4 import BeautifulSoup
DEBUG = False
TITLE = "url2title.py"
MYURL = "https://daisuke20240310.hatenablog.com"
def get_title( url ):
try:
response = requests.get( url )
response.raise_for_status()
soup = BeautifulSoup( response.text, 'html.parser' )
title = soup.title.text.strip()
if DEBUG: print( f"URL: {url} - ã¿ã¤ãã«: {title}" )
return title
except Exception as e:
print(f"ã¨ã©ã¼ãçºçãã¾ãã: {e}")
def main():
layout = [ [eg.Text("", key='text')] ]
window = eg.Window( TITLE, layout, size=(700, 60) )
getdata = pyperclip.paste()
getdata_old = ""
while True:
event, values = window.read( timeout=1000 )
if event != "__TIMEOUT__" and event != "-TIMEOUT-":
if event == eg.WIN_CLOSED:
break
getdata = pyperclip.paste()
if getdata == getdata_old:
continue
else:
txt = getdata
if MYURL in getdata:
title = get_title( getdata )
if DEBUG: print( f"getdata={getdata}, title={title}" )
if title is not None:
txt = "URLï¼" + getdata + "\n" + "ã¿ã¤ãã«ï¼" + title
window['text'].update( txt )
getdata_old = getdata
window.close()
if __name__ == '__main__':
main()
å
¨ä½ã®æµãã¨ãã¦ã¯ãçãä¸ãããã«ãããwhile True:ãã®ç¡éã«ã¼ãããã£ã¨åã£ã¦ãã¾ãã
- window.read()ï¼1ç§ãã¨ã«ã¯ãªãããã¼ãã確èªããããã«èµ·ãä¸ããã¾ã
- getdata = pyperclip.paste()ï¼ã¯ãªãããã¼ãããå
容ãåå¾ãã¾ã
- if MYURL in getdata:ï¼åå¾ããURLãèªåã®ãµã¤ããå«ãã§ãããã©ãããå¤å®ãã¦ãã¾ã
- get_title()ï¼ã¹ã¯ã¬ã¤ãã³ã°ã®é¢æ°ãã³ã¼ã«ãã¦ãã¾ã
- ã¿ã¤ãã«ãåå¾ã§ããããURLã¨çµã¿åããã¦ã2è¡ã®æååãä½ã£ã¦ãGUIãã¼ãã«åæ ãã¾ã
ç°¡åã§ãã説æã¯ä»¥ä¸ã§ãã
ããããããã®ã½ã¼ã¹ã³ã¼ãã¯èªç±ã«å¤æ´ãã¦ä½¿ã£ã¦å¤§ä¸å¤«ã§ããä¸å¿ãåé
å¸ã¯ãã¡ã¨ãããã¨ã«ãã¦ããã¾ãã
ã¯ãªãããã¼ãã使ã£ãã¢ããªã¯ãã¢ã¤ãã¢æ¬¡ç¬¬ã§ãã¨ã¦ã便å©ãªãã®ã«ãªãã¨æãã®ã§ãæ´»ç¨ãã¦é ãããã¨æãã¾ãã
ä¾ãã°ãæ¥ãã¾ããæéã®å·®ã®è¨ç®ï¼3æ¥åã®22:43ããä»æ¥ã®3:05ã¾ã§ã¨ãï¼ã£ã¦ãæç®ã§è¨ç®ããã®ã¯å°ãé¢åã§ãããã2ã¤ã®æå»ä¸¦ã¹ã¦æ¸ãã¦ãããã³ãã¼ããã ãã§æéå·®ã表示ããã¢ããªãä½ã£ã¦ããã¨ä¾¿å©ã§ãã
ã¾ããåæ¦ã¨è¥¿æ¦ã®å¤æã¨ããæå®ããã¢ã¹ãã¼ã³ã¼ãããå¤ã表示ããã¨ãã便å©ããã§ãã
çµããã«
ä»åã¯ãPythonã§ãç°¡åãªGUIã¢ããªãä½ã£ã¦ã¿ã¾ããã
Google Search Consoleã§ãURLãè¦ãã ãã§ã¯ãä½ã®ãã¼ã¸ãåãããããã¡ãã¡ã¢ã¯ã»ã¹ãã¦ç¢ºèªããã®ãé¢åã ã£ãã®ã§ä½ã£ã¦ã¿ã¾ããã
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã