-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscraper.py
51 lines (40 loc) · 1.22 KB
/
scraper.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
import requests
from bs4 import BeautifulSoup as BS
from models import QueryResults
from django.db.utils import OperationalError
#import os
def get_data(query):
#taken youtube web API and added our query string to it
url = 'https://www.youtube.com/results?search_query=' + query
r = requests.get(url)
html = r.text
soup = BS(html)
vids = soup.find_all('a', class_='yt-uix-tile-link')
num = 1
#save the query results in the databse
for ix in vids:
#print num, ix.text #, ix['href']
new_data_element = QueryResults(sno = num, text = ix.text)
new_data_element.save()
num += 1
#get all data from the database to display it
all_data = QueryResults.objects.all()
data = ''
for dt in all_data:
data = data + str(dt) + '\n'
return data
"""
try:
index = int(raw_input('Enter the S.NO. of the video that you would like to download:'))
except (EOFError):
print "error!!"
video_url = 'https://www.youtube.com' + vids[index]['href']
#print video_url
#os.system('youtube-dl ' + video_url)
"""
get_data(query= "goong english subtitles")
#youtube-dl downloads a video to our system locally.
#command: youtube-dl video-url
def get_video(index):
video_url = 'https://www.youtube.com' + vids[index]['href']
return video_url