크롤링 퀴즈 : 순위 영화제목 평점만 출력되게 하기
다음은 내가 짠 코드
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=pnt&date=20200303',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select('#old_content > table > tbody > tr')
for tr in trs:
a_tag = tr.select_one('td.title > div > a')
b_tag = tr.select_one('td:nth-child(1) > img')
c_tag = tr.select_one('td.point')
if a_tag and b_tag and c_tag is not None:
movie = b_tag['alt'] + ' ' + a_tag.text + ' ' + c_tag.text
print(movie)
아래는 답안
import requests
from bs4 import BeautifulSoup
# URL을 읽어서 HTML를 받아오고,
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('<https://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=pnt&date=20200303>',headers=headers)
# HTML을 BeautifulSoup이라는 라이브러리를 활용해 검색하기 용이한 상태로 만듦
soup = BeautifulSoup(data.text, 'html.parser')
# select를 이용해서, tr들을 불러오기
movies = soup.select('#old_content > table > tbody > tr')
# movies (tr들) 의 반복문을 돌리기
for movie in movies:
# movie 안에 a 가 있으면,
a_tag = movie.select_one('td.title > div > a')
if a_tag is not None:
rank = movie.select_one('td:nth-child(1) > img')['alt'] # img 태그의 alt 속성값을 가져오기
title = a_tag.text # a 태그 사이의 텍스트를 가져오기
star = movie.select_one('td.point').text # td 태그 사이의 텍스트를 가져오기
print(rank,title,star)
뭐야 왜 이렇게 달라^.T....
수정해보니 b_tag랑 c_tag를 지워도 결과가 똑같이 나오긴 하더라.
똑같은 tr에서 돌리는 거라 조건 결과가 동일해서 그런 것 같다.
변수를 지정해주는 쪽이 코드가 더 깔끔하긴 해서.....귀찮아도 선언해주는 습관을 들여야겠다.
pymongo 공부 중
데이터 결과값을 찾는 find 기본 코드 중
{'_id':False} 는 데이터에서 id값은 나타내지 말라는 의미.
(id는 랜덤으로 생성되는 유니크값이다.)
find_one을 사용할 경우 값이 여러 개가 있어도 제일 위의 데이터만 가져온다.
update 코드
db.people.update_many(찾을조건,{ '$set': 어떻게바꿀지 })
(예시) db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
ㄴname이 bobby인 애를 찾아서 age를 19로 바꾸라는 뜻.
update_many는 name이 bobby인 애를 모두 찾아서 전부 age를 19로 바꾸게 된다.
delete도 delete_many가 있지만 update_many와 마찬가지로 잘 쓰진 않는다.
(잘못하면 큰일나니까)
웹스크래핑 퀴즈 : 매트릭스와 평점이 같은 영화 제목들을 가져오기
내가 짠 코드
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta
all_movies = list(db.movies.find({}))
target = db.movies.find_one({'title':'매트릭스'})
for star in all_movies:
same_star = star['star']
if same_star == target['star']:
print(star['title'])
답안
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta
## 코딩 할 준비 ##
target_movie = db.movies.find_one({'title':'매트릭스'})
target_star = target_movie['star']
movies = list(db.movies.find({'star':target_star}))
for movie in movies:
print(movie['title'])
결과는 같은데 과정이 많이 다르군!!!!
리스트를 어떻게 뽑느냐의 차이인 것 같다.
리스트 안에 딕셔너리가 있는 경우
그냥 print(movie)로 출력하면 가로로 쭉 나열되기 때문에 보기가 어려우므로
반복문 for을 사용해서 한 줄씩 세로로 나타나게 하는 게 편하다.
양쪽 공백 제거는 .strip()
문자열 자르기는 슬라이싱 기능을 이용하면 되고 https://wikidocs.net/2838 이쪽을 참고하자.
숙제 완료한 코드는 아래.
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://www.genie.co.kr/chart/top200?ditc=D&ymd=20200403&hh=23&rtm=N&pg=1',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
for tr in trs:
title = tr.select_one('td.info > a.title.ellipsis').text.strip()
rank = tr.select_one('td.number').text[0:2].strip()
singer = tr.select_one('td.info > a.artist.ellipsis').text
print(rank, title, singer)
'스파르타 코딩클럽' 카테고리의 다른 글
2022.01.16 (0) | 2022.01.16 |
---|---|
2022.01.14 (0) | 2022.01.14 |
2022.01.12~13 (0) | 2022.01.12 |
2022.01.07 (0) | 2022.01.07 |
2022.01.05 (0) | 2022.01.05 |
2022.01.03 (0) | 2022.01.03 |
2022.01.02 (0) | 2022.01.02 |
2022.01.01 (0) | 2022.01.01 |