-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
169 lines (122 loc) · 4.12 KB
/
parser.py
File metadata and controls
169 lines (122 loc) · 4.12 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pafy
import requests
from flask import session
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
}
def parse(query, page):
s = requests.Session()
r = s.get('https://youtube.com/results?search_query=' + query, headers=headers)
cookies = requests.utils.dict_from_cookiejar(s.cookies)
# print(cookies['YSC'])
# print(cookies['PREF'])
# print(cookies['VISITOR_INFO1_LIVE'])
session['YSC'] = cookies['YSC']
session['PREF'] = cookies['PREF']
session['VISITOR_INFO1_LIVE'] = cookies['VISITOR_INFO1_LIVE']
# {'YSC': 'G0cfNqwweIs', 'PREF': 'f1=50000000', 'VISITOR_INFO1_LIVE': '_ifQmz45RQw'}
soup = BeautifulSoup(r.text)
uniqueIdList = []
dataList = dict()
# Youtube 고유 ID를 가진 div 태그를 가져옴
for div in soup.find_all('div'):
if div.get('data-context-item-id') is not None:
uniqueIdList.append(div.get('data-context-item-id'))
for uniqueId in uniqueIdList:
hasException = False
try:
raw = pafy.new('https://youtube.com/watch?v=' + uniqueId)
title = raw.title
author = raw.author
length = raw.length
url = raw.audiostreams[0].url
thumb = raw.bigthumb
except:
hasException = True
pass
finally:
if hasException or (title == '' or author == '' or length == '' or length == 0 or url == '' or thumb == ''):
pass
else:
dataList[uniqueId] = {'title': title, 'imgSrc': thumb, 'author':author, 'duration':length, 'url': url}
# dataList[uniqueId] = {'title': 'title', 'imgSrc': 'thumb', 'author':'author', 'length':'length', 'url': 'url'}
# aList = []
# # 썸네일 이미지와 비디오 이름을 가진 a 태그를 가져옴
# for a in soup.find_all('a'):
# for data in divList:
# if a.get('href') == ('/watch?v=' + data):
# aList.append(a)
# data = dict()
# imgList = []
# textList = []
# for a in aList:
# img = ''
# if a.img is not None:
# if 'data-thumb' in a.img.attrs:
# img = a.img['data-thumb']
# elif 'src' in a.img.attrs:
# img = a.img['src']
# imgList.append(img)
# else:
# textList.append(a.text)
# for count in range(len(divList)):
# videoId = divList[count]
# imgSrc = imgList[count]
# text = textList[count]
# data[videoId] = {'title': text, 'imgSrc': imgSrc}
# # video = pafy.new('https://youtube.com/watch?v=' + data[0])
# # print(aList)
# # print(data)
return dataList
# def parsing(page):
# s = requests.Session()
# r = s.get('https://youtube.com/results?search_query=Epik+High?page='+page, headers=headers)
# cookies = requests.utils.dict_from_cookiejar(s.cookies)
# # print(cookies['YSC'])
# # print(cookies['PREF'])
# # print(cookies['VISITOR_INFO1_LIVE'])
# session['YSC'] = cookies['YSC']
# session['PREF'] = cookies['PREF']
# session['VISITOR_INFO1_LIVE'] = cookies['VISITOR_INFO1_LIVE']
# # {'YSC': 'G0cfNqwweIs', 'PREF': 'f1=50000000', 'VISITOR_INFO1_LIVE': '_ifQmz45RQw'}
# soup = BeautifulSoup(r.text)
# divList = []
# # Youtube 고유 ID를 가진 div 태그를 가져옴
# for div in soup.find_all('div'):
# if div.get('data-context-item-id') is not None:
# divList.append(div.get('data-context-item-id'))
# for div
# if div.get('class') == 'yt-lockup-byline':
# uploaderList.append(div.get('class'))
# aList = []
# # 썸네일 이미지와 비디오 이름을 가진 a 태그를 가져옴
# for a in soup.find_all('a'):
# for data in divList:
# if a.get('href') == ('/watch?v=' + data):
# aList.append(a)
# data = dict()
# imgList = []
# textList = []
# uploaderList = []
# for a in aList:
# img = ''
# if a.img is not None:
# if 'data-thumb' in a.img.attrs:
# img = a.img['data-thumb']
# elif 'src' in a.img.attrs:
# img = a.img['src']
# imgList.append(img)
# else:
# textList.append(a.text)
# for count in range(len(divList)):
# videoId = divList[count]
# imgSrc = imgList[count]
# text = textList[count]
# data[videoId] = {'title': text, 'imgSrc': imgSrc}
# # video = pafy.new('https://youtube.com/watch?v=' + data[0])
# # print(aList)
# # print(data)
# return data