こんにちはみなさん。
ようこそチョビブログへ。
今回はPythonのスクレイピングでDescriptionを取得する方法を紹介します。
結構このタグの中身欲しいときありますよね。
必要なライブラリ
pip install requests
pip install beautifulsoup4
Python3 コード
import requests
from bs4 import BeautifulSoup
load_url = "https://chobisun.com/?p=22"
html = requests.get(load_url)
soup = BeautifulSoup(html.content, "html.parser")
foundForDesc = soup.find('meta', attrs={'name': 'description'})
description = foundForDesc.get("content")
print(description)
実行結果
PS C:\Users\chobi\Documents\chobisunBlg> python3 .\getMetaTagDesc.py
Chromeの新規タブで直接Weblioに行けるようにしたら英単語の検索がすごく早くなりました。設定方法などを紹介しています。英単語を検索することが多い人は必見!
実行環境は
- Python 3.9.10
- requests 2.25.1
- beautifulsoup4 4.10.0
おまけ
metaタグのほかの情報も取れるよ
import requests
from bs4 import BeautifulSoup
load_url = "https://chobisun.com/?p=22"
html = requests.get(load_url)
soup = BeautifulSoup(html.content, "html.parser")
foundForkeywords = soup.find('meta', attrs={'name': 'keywords'})
keywords= foundForkeywords.get("content")
print(keywords)