Programming/python

bs4를 이용한 웹 크롤링 (파싱)

범고래_1 2019. 1. 14. 12:11

html은 requests 모듈을 이용해서 가져오면되고,

그다음에 원하는 데이터만을 뽑아내야 하는데,

크롬 개발자 도구와 CSS의 selector를 이용하면 개꿀빨 수 있다.

아래와 같이 말이다.


import requests
from bs4 import BeautifulSoup

url = 'https://bluebottlecoffee.com/store/coffee'

r = requests.get(url)
page = r.text.encode('utf8')

soup = BeautifulSoup(page, 'html.parser')

my_products = soup.select(
    '#heap-product-card > div.dn.df-s.flex-col.grid-col-1.transitional-silver-shadow > div.flex-1-0 > div.tc.ph10 >
)

for x in my_products:
    print x.text


'Programming > python' 카테고리의 다른 글

matplotlib에서 show() 할 때 빈 화면 해결  (7) 2019.07.11
파이썬에서 한글 인식  (0) 2019.01.14
python 정수형 signed, unsigned 변환  (0) 2018.12.18
python md5, sha256  (0) 2018.08.19
requests 모듈  (0) 2017.01.11