데이터 분석/EDA_웹크롤링_파이썬프로그래밍
[WebData][chicago] 4. 하위페이지 분석
ruby-jieun
2023. 2. 5. 19:09
웹 데이터 수집하고 정리하기
시카고 맛집 데이터 분석
4. 하위페이지 분석
1.
- 50 URL 중 하나를 대상으로 잡는다.
- 해당 페이지를 확인해보자.
import pandas as pd
from urllib.request import urlopen, Request
from fake_useragent import UserAgent
from bs4 import BeautifulSoup
df = pd.read_csv("../data/03. best_sandwiches_list_chicago.csv", index_col=0)
df.tail()
df["URL"][0]
2.
- p 태그에 addy라는 class에 내가 얻고 싶은 정보가 있다.
3. 가격만 가져오고 싶은데.. 가격과 주소가 같이 있다.
req = Request(df["URL"][0], headers={"user-agent":ua.ie})
html = urlopen(req).read()
soup_tmp = BeautifulSoup(html, "html.parser")
soup_tmp.find("p", "addy")
4. 살펴보자
- 먼저 $ 달러 기호를 만나서 나타나는 숫자들은 .을 만날 때까지 가격이다.
- 단 10.5처럼 . 기호 후에 다시 연달아 숫자가 나올 수도 있다.
- 띄어쓰기 후에 숫자 혹은 문자가 나타나면 주소이다.
- 주소는 .,으로 끝난다.
<p class="addy">
<em>$10. 2109 W. Chicago Ave., 773-772-0406, <a href="http://www.theoldoaktap.com/">theoldoaktap.com</a></em></p>