상세 컨텐츠

본문 제목

메타태그 크롤링 하는 법

백엔드/파이썬

by 호치민 개발자 2023. 2. 24. 18:01

본문

import requests
from bs4 import BeautifulSoup

url = 'https://movie.naver.com/movie/bi/mi/basic.nhn?code=171539'

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get(url,headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')

# 여기에 코딩을 해서 meta tag를 먼저 가져와보겠습니다.
# 이 방법대로 하면 안나옴
# title = soup.select_one('head > meta:nth-child(9)')


title = soup.select_one('meta[property="og:title"]')['content']
image = soup.select_one('meta[property="og:image"]')['content']
desc = soup.select_one('meta[property="og:description"]')['content']
print(title, image, desc)

크롤링이 수공업이라고 누가 말했던가.. 

정말 명언이다 

 

사이트 마다 / 데이터 마다 하나하나 방법이 다르고,,, 메타태그를 활용한 방법은 이번에 알게 되었다.!!

관련글 더보기