-
웹개발 4주차 - 2 (메타태그)Python 2023. 5. 3. 10:35
스파르타피디아 메타태그
1. templates 에 index.html 파일.
2. python -m venv venv 설치, app.py파일. (interpreter를 venv로 잡아주고 새터미널)
3. pip install flask pymongo dnspython requests bs4 설치.
4. meta_prac.py설치 후 아래입력.
import requestsfrom bs4 import BeautifulSoup
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')
ogtitle = soup.select_one('meta[property="og:title"]')['content']ogimage = soup.select_one('meta[property="og:image"]')['content']ogdesc = soup.select_one('meta[property="og:description"]')['content']-> 메타태그에서 ogtitle 가져오는법. select copy도 되지만 이렇게 해도 됨.print(ogtitle,ogimage,ogdesc)url 들어가서 마우스 우클릭 검사-요소 들어가서 <head>보면 <meta property> 나오는데, 이게 메타태그
'Python' 카테고리의 다른 글
웹개발 4주차 (백엔드-프론트엔드, 프로젝트1 : 화성땅 공동구매) (0) 2023.05.02 웹개발 3주차 (파이썬, 크롤링, 지니뮤직 예제) (0) 2023.05.01 웹개발 2주차 (Java Script, JQuery, Fetch) (0) 2023.04.27 웹개발 1주차 (서버-클라이언트, HTML, CSS) (0) 2023.04.26