파이썬

개발일지 8일차

kingsgirl 2023. 3. 3. 17:57
728x90

파이썬 크롤링 

스타벅스 지역 검색 해서 해당 시 도 구 에 존재하는 매장보기

#스타벅스매장지점 크롤링
from selenium import webdriver
from bs4 import BeautifulSoup
import time
from selenium.webdriver.common.by import By
import time

url = 'https://www.starbucks.co.kr/store/store_map.do'
driver = webdriver.Chrome()
driver.get(url)
time.sleep(5)

loca = driver.find_element(By.CLASS_NAME,'loca_search')
loca.click()
time.sleep(5)

sido = driver.find_elements(By.CSS_SELECTOR,'ul.sido_area_box > li')
# print(sido)
sido[5].click()     #여기에서 오류가 난다 
time.sleep(5)

gugun = driver.find_elements(By.CSS_SELECTOR,'ul.gugun_area_box > li')
print(sido)
gugun[-1].click()
time.sleep(5)

page = driver.page_source

soup = BeautifulSoup(page,'html.parser')
item = soup.select('ul.quickSearchResultBoxSidoGugun li')
for li in item:
    print(li.fine('strong').string)
    print(li.find('p').text)
    print('---------------------')


  #  IndexError: list index out of range

저기 sido[5] 이 부분에서 에러가 난다 . 왜지 ?? 해결 하기 ..

 

 

 

->3/4 이부분에서 에러가 났던거 ... 

ul.gugun_area_box     area - > arae 였고 , 마지막 print문에 fine 이 아니라 find 였음 

 

 

제대로 출력되는 코드 ( 코드에 정답이 어딨어 ) 

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

url = 'https://www.starbucks.co.kr/store/store_map.do'
driver = webdriver.Chrome()
driver.get(url)
time.sleep(5)

loca = driver.find_element(By.CLASS_NAME,'loca_search')
loca.click()
time.sleep(5)

sido = driver.find_elements(By.CSS_SELECTOR,'ul.sido_arae_box > li')
# print(sido)
sido[2].click()
time.sleep(5)

gugun = driver.find_elements(By.CSS_SELECTOR,'ul.gugun_arae_box > li')
# prit(sido)
gugun[3].click()
time.sleep(5)

page = driver.page_source

soup = BeautifulSoup(page,'html.parser')
item = soup.select('ul.quickSearchResultBoxSidoGugun li')
for li in item:
    print(li.find('strong').string)
    print(li.find('p').text)
    print('--------------------------------')

 

 

----------------------------------------------------------------------------------------

 1. 네이버 환율 

import requests
from bs4 import BeautifulSoup

url = 'http://finance.naver.com/marketindex/exchangeList.naver'
re = requests.get(url).text
soup = BeautifulSoup(re,'html.parser')
name = []
price = [] 

data = soup.select('td.tit a')
#print(data)
for item in data:
    name.append(item.string.strip())
print(name)

data = soup.select('td.sale')
for item in data:
    price.append(float(item.string.replace(',','')))
print(price)
items = list(zip(name,price))  # zip으로 묶고자하는 name과 price
print(items)

나라 , 환율 나타내고 마지막에 나라와 환율 같이 나타냄 

 

 

2. cgv 영화차트 영화 제목 크롤링 

import requests
from bs4 import BeautifulSoup

url = 'http://www.cgv.co.kr/movies/?lt=1&ft=0'
re = requests.get(url).text 
soup = BeautifulSoup(re, 'html.parser') 
name = []

data = soup.select('strong.title')
#클래스는 . 

for item in data:
    name.append(item.string.strip())
print(name)

 

 

3. 벅스에서 노래 제목 , 가수 이름 

url = 'https://music.bugs.co.kr/chart'
response = request.urlopen(url)
soup = BeautifulSoup(response,'html.parser')

title_list = []
artist_list = []

title= soup.select('p.title > a')
#' > a ' :바로 밑에있는 a 태그 
print(title)
for item in title:
    # print(item.string)  #글자만 추출 string
    title_list.append(item.string)

artist = soup.select('p.artist > a')
for item in artist:
    # print(item.string)
    artist_list.append(item.string.strip())

result = list(zip(title_list,artist_list))
print(result)

 

4. 네이버 영화리뷰에서 1페이지부터 10페이지 까지 영화 제목 , 평점, 영화평  크롤링

for i in range(1,11):
    url = f'https://movie.naver.com/movie/point/af/list.naver?&page=[i]'
    response = request.urlopen(url)
    soup = BeautifulSoup(response,'html.parser')
    print(soup)
    table = soup.find('table',class_='list_netizen')
    for i,r in enumerate(table.select('tbody tr')):
        print(i)
        print(r)
        for j,c in enumerate(r.fine_all('td')):
            print(j)
            print(c)
            movie_dic={'제목':'','평점':'','영화평':''}
            if j == 0:
                movie_dic['글번호'] = int(c.string.strip())
            elif j == 1:
                print('제목 : ',c.select_one('a').string)
                title = c.select_one('a').stirng.strip()
                print(c.select_one('em').string)
                score = c.select_one('em').string
                print(c.text)
                review = c.text
                review = review.replace(title,'')
                review = review.replace('신고','')   #신고지우기
                review = re.sub('별점 -총 10 점중 [0-9]{1,2}','',review).strip()
                print('영화평 : ', review)

            movie_dic = {'제목' :title , '평점':score,'영화평':review}
            try:
                movie_dic = {'제목':record1, '점수'}

미완성이다 . 

728x90