forked from dilrajsingh1997/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytube_results.py
31 lines (25 loc) · 863 Bytes
/
ytube_results.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
import urllib2
import os
from bs4 import BeautifulSoup
def get_videos(query,nresults):
vid_data=[]
url = "https://www.youtube.com/results?search_query=" + query.replace(' ','+')
for _ in range(1000):
response = urllib2.urlopen(url)
html = response.read()
soup = BeautifulSoup(html,'lxml')
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
data={}
data['url']='https://www.youtube.com' + vid['href']
data['info']=vid.get_text().encode("utf-8")
vid_data.append(data)
if(len(vid_data)>nresults):
break
if(len(vid_data)>nresults):
break
return vid_data
if __name__ == "__main__":
query =raw_input("Enter search text:")
videos=get_videos(query,10)
for video in videos:
print video['info'],video['link']