Last active
February 14, 2019 11:37
-
-
Save developer-sdk/3fe8a177f549ac70c4c1a8b515dccb7b to your computer and use it in GitHub Desktop.
BeautifulSoup 예제
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| ''' | |
| Created on 2019.02.09 | |
| @author: whitebeard-k | |
| ''' | |
| import requests | |
| from bs4 import BeautifulSoup | |
| def main(): | |
| html_url = "https://news.naver.com/main/list.nhn?mode=LSD&mid=sec&sid1=105" | |
| r = requests.get(html_url) | |
| #bs = BeautifulSoup(r.text, 'html.parser') # 기본 파서 | |
| bs = BeautifulSoup(r.text, 'lxml') # lxml 파서 | |
| #print(bs.prettify()) | |
| # li 엘리먼트 검색 | |
| for li in bs.find_all('li'): | |
| print(li.prettify()) | |
| # type06_headline클래스를 가지는 ul엘리먼트를 찾아서 li만 검색 | |
| ul = bs.find("ul", class_="type06_headline") | |
| for li in ul.find_all("li"): | |
| print(li.prettify()) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment