from urllib.request import urlopen
url = “http://info.finance.naver.com/marketindex/”
page = urlopen(url)
soup = BeautifulSoup(page, “html.asrser”)
print(soup.prettify())
import urllib
from urllib.request import Request
html = “https://ko.wikipedia.org/wiki/{search_words}”
req = Request(html.format(search_words=urllib.parse.quote(“여명의_눈동자”)))
response = urlopen(req)
soup = BeautifulSoup(response, “html.parser”)
soup
n = 0
for each in soup.find_all(‘ul’):
print(“=>” + str(n) + “================”)
print(each)
n += 1
soup.find_all(“ul”)[2]
print(colors[0])
print(colors[1])
print(len(colors))
014-016. 시카고 맛집 메인페이지 분석
from urllib.request import Request, urlopen
url_base = “http://www.chicagomag.com”
url_sub = “/Chicago-Magazine/Novemver=2012/Best-Sandwiches-Chicago/”
url = url_base + url_sub
req = Request(url, headers={“User-Agent”: “Chrome”})
html = urlopen(req).read()
soup = BeautifulSoup(html, “html.parser”)
soup
print(soup.find_all(“div”, “sammy”))
len(soup.find_all(“div”, “sammy”))
# 50개가 제대로 들어온 것을 확인하기
print(soup.find_all(“div”, “sammy”)[0])
tmp_one = soup.find_all(“div”, “sammy”)[0]
type(tmp_one)
# type이 bs4.element.Tag라는 것은 find명령을 사용할 수 있다는 뜻
tmp_one.find(class_=“sammyRank”)
tmp_one.find(class_=“sammyRank”).get_text()
# 랭킹 데이터 확보
tmp_one.find(class_=“sammyListing”).get_text()
# 가게 이름과 메뉴 데이터가 한번에 있다.
tmp_one.find(“a)[”href“]
# 연결되는 홈페이지 주소가 ‘상경대로’임
import re
tmp_string = tmp_one.find(class_=“sammyListing”).get_text()
re.split((“역슬래쉬n|역슬래쉬r역슬래쉬n”), tmp_string)
#가게 이름과 메뉴는 re 모듈의 split으로 쉽게 구분할 수 있다.