ユーザ用ツール

サイト用ツール


python_beautifulsoup

BeautifulSoup

  • HTMLを解析できる

import requests
from bs4 import BeautifulSoup
 
def main():
 
    target = 'TARGET_URL'
 
    soup = BeautifulSoup(
        requests.get(target).content,
        'html.parser'
    )
 
    title = soup.find('title').text
 
    print('title:', title)
 
    if (len(title.split(' - ')) != 1):
 
        print('page:', title.split(' - ')[-1])
 
    for elem in soup.find_all('meta', attrs={'name': 'author'}):
 
        print('author:', elem['content'])
 
    for elem in soup.find_all('meta', attrs={'name': 'description'}):
 
        print('description:', elem['content'])
 
    print('url:', target)
 
 
if (__name__ == '__main__'):
 
    main()
python_beautifulsoup.txt · 最終更新: 2021/06/15 22:55 by 127.0.0.1