forked from HANGHAE99-Group-04-Study/Sorting_Hat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
showing.py
63 lines (55 loc) · 2.24 KB
/
showing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re
import certifi
import os
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
import crawling
client = MongoClient(os.environ.get("MONGO"), tlsCAFile=certifi.where())
db = client.Sorting_Hat_Dev
db.movies.update_many({}, {"$set": {'showing': 0}}) # DB에 있는 전체 영화 상영여부 False로 변환
# ------------URL 변수-------------------
current_url = 'https://movie.naver.com/movie/running/current.naver' # 현재 상영작
premovie_url = 'https://movie.naver.com/movie/running/premovie.naver' # 상영 예정작
def get_url(url, tag):
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(url, headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
# select를 이용해서, li들을 불러오기
movies = soup.select('#content > div.article > div > div.lst_wrap > ul > li')
# movies (li들) 의 반복문을 돌리기
for movie in movies:
m_id = re.search(r'(?<=code=)(.*?)$', movie.select_one('dl > dt > a')['href']).group() # 영화별 고유 ID값
m_title = movie.select_one('dl > dt > a').text # 영화제목
doc = crawling.get_all(m_id, m_title, tag)
print(doc)
db.movies.update_one(
{
'_id': doc['id'],
},
{
"$set": {
'user_star': doc['user_star'],
'image': doc['image'],
'reviewer_star': doc['reviewer_star'],
'showing': doc['showing']
},
"$setOnInsert": {
'_id': doc['id'],
'title': doc['title'],
'age': doc['age'],
'genre': doc['genre'],
'video_url': doc['video_url'],
'release': doc['release'],
'nation': doc['nation'],
'time': doc['time'],
'director': doc['director'],
'actor': doc['actor'],
'tx': doc['tx'],
},
},
upsert=True
)
get_url(current_url, 1)
get_url(premovie_url, 2)