시카고 맛집 메인페이지 분석
find_all
로 "div"
태그의 "sammy"
클래스 검색select
같은 작업 가능 soup.find_all("div", "sammy"), len(soup.find_all("div","sammy))
soup.select("div","sammy"), len(soup.select("div","sammy))
tmp_one = soup.find_all("div","sammy)[0]
type(tmp_one), tmp_one
tmp_one.find(class_:"sammyRank").get_text()
#tmp_one.select_one(".sammyRank").text
#tmp_one.find("div", {"class":"sammyListing"})
tmp_one.fine("div", "{"class":"sammyListing"}).get_text()
#tmp_one.select_one(".sammyListing").text
tmp_one.find("a")["href"] #태그 a 안에 속성값 href 확인(url주소)
#tmp_one.select_one("a").get("href")
import re
tmp_string = tmp_one.find(class_="sammyListing").get_text()
tmp_string
re.split(("\n|\r\n), tmp_string)
print(re.split(("\n|\r\n"), tmp_string)[0]) #menu
print(re.split(("\n|\r\n"), tmp_string[1]) #cafe
from urllib.parse import urljoin
url_base = 'https://www.chicagomag.com'
#필요한 내용을 담을 빈 리스트
#리스트로 하나씩 컬럼을 만들고, DataFrame으로 합칠 예정
rank = []
main_menu = []
cafe_name = []
url_add = []
list_soup = soup.find_all("div", "sammy")#soup.select(".sammy")
for item in list_soup:
rank.append(item.find(class_="sammyRank").get_text())
tmp_string = item.find(class_="sammyListing").get_text()
main_menu.append(re.split(("\n|\r\n"), tmp_string)[0])
cafe_name.append(re.split(("\n|\r\n"), tmp_string)[1])
url_add.append(urljoin(url_base, item.find("a")["href"]))
len(rank), len(main_menu), len(cafe_name), len(url_add)
rank[:5]
main_menu[:5]
, cafe_name[:5]
, url_add[:5]
확인
데이터 프레임 정리
import pandas as pd
data = {
"Rank": rank,
"Menu":main_menu,
"Cafe":cafe_name,
"URL":url_add
}
df = pd.DataFrame(data)
df.tail(2)
df = pd.DataFrame(data, columns=["Rank","Cafe","Menu","URL"])
df.tail()
df.to_csv(
"../data/03. best_sandwiches_list_chicago.csv", sep=",", encoding="utf-8"
)
시카고 맛집 데이터 하위 페이지 분석
import pandas as pd
from urllib.request import urlopen, Reqeust
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
df = pd.read_csv("../data/03. best_sandwiches_list_chicago.csv",index_col=0)
df.tail()
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") #soup_tmp.select_one(".addy")
.x
: 임의의 한 문자를 표현(x가 마지막으로 끝납니다)
x+
: x가 1번 이상 반복합니다.
x?
: x가 존저해거나 존재하지 않습니다.
x*
: x가 0번이상 반복합니다.
x|y
: x 또는 y를 찾습니다(or 연산자를 의미합니다)
price_tmp = soup_tmp.find("p", "addy").get_text()
print_tmp
import re
re.split(".,", price_tmp)
price_tmp = re.split(".,", price_tmp)[0]
price_tmp
__________________________________________
<결과값> '\n$10. 2109 W. Chicago Ave'
re.search("\$\d+\.(\d+)?", price_tmp).group()
______________________________________________
<결과값> $10
tmp = re.search("\$\d+\.(\d+)?", price_tmp).group()
price_tmp[len(tmp) + 2:]
___________________________________________________
<결과값> '2109 W. Chicago Ave'
price = []
address = []
for n in df.index[:3]:
req = Request(df['URL'][n], headers={"user-Agent":ua.ie})
html = urlopen(req).read()
soup_tmp = BeautifulSoup(html, "html.parser")
gettings = soup_tmp.find("p", "addy").get_text()
price_tmp = re.split(".,", gettings)[0]
tmp = re.search("\$\d+\.(\d+)?", price_tmp).group()
price.append(tmp)
address.append(price_tmp[len(tmp)+2:]
print(n)
price, address
from tqdm import tqdm
from tqdm import tqdm
price = []
address = []
for idx, row in tqdm(df.iterrows()):
req = Request(row['URL'][n], headers={"user-Agent":ua.ie})
html = urlopen(req).read()
soup_tmp = BeautifulSoup(html, "html.parser")
gettings = soup_tmp.find("p", "addy").get_text()
price_tmp = re.split(".,", gettings)[0]
tmp = re.search("\$\d+\.(\d+)?", price_tmp).group()
price.append(tmp)
address.append(price_tmp[len(tmp)+2:]
print(idx)
길이 확인
len(price), len(address)
price[:5]
address[:5]
원본 데이터프레임과 합치기
df["Price"] = price
df["Address"] = address
df = df.loc[:, ["Rank", "Cafe", "Menu", "Price", "Address"]]
df.set_index("Rank", inplace=True
df.head()
df.to_csv(
"../data/03. best_sandwiches_list_chicago2.csv", sep=",", encoding="UTF-8"
)
pd.read_csv("../data/03. best_sandwiches_list_chicago2.csv", index_col=0)
시카고 맛집 데이터 지도 시각화
import folium
import pandas as pd
import numpy as np
import googlemaps
from tqdm import tqdm
df = pd.read_csv("../data/03. best_sandwiches_list_chicago2.csv", index_col=0)
df.tail(5)
gmaps_key = "구글키 입력"
gmaps = googlemaps.Client(key=gmaps_key)
lat = []
lng = []
for idx, row in tqdm(df.iterrows()):
if not row["Address"] == "Multiple location":
target_name = row["Address"] + ", " + "Chicago"
#print(target_name)
gmaps_output = gmaps.geocode(target_name)
location_output = gmaps_output[0].get("geometry"))
lat.append(location_output["location"]["lat"])
lat.append(location_output["location"]["lng"])
#location_output = gmaps_output[0]
else:
lat.append(np.nan)
lng.append(np.nan)
df["lat"] = lat
df["lng"] = lng
df.tail()
mapping = folium.Map(location=[41.8781136, -87.6297982], zoom_start=11)
for idx, row in df.iterrows():
if not row["Address"] == "Multiple location":
folium.Marker(
location=[row["lat"], row["lng"]],
popup=row["Cafe"]
tooltip=row["Menu"], #아이콘커스텀
icon=folium.Icon(
icon="coffee"
prefix="fa"
)
).add_to(mapping)
mapping
공감하며 읽었습니다. 좋은 글 감사드립니다.