Created
December 12, 2017 18:06
-
-
Save tjoen/2acf04b6bd7e9930dd23a87fc9682c15 to your computer and use it in GitHub Desktop.
Handy functions to get links to images and videos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from bs4 import BeautifulSoup | |
import urllib2 | |
request_headers = { | |
"Accept-Language": "en-US,en;q=0.5", | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", | |
"Referer": "http://thewebsite.com", | |
"Connection": "keep-alive" | |
} | |
def google_image(x): | |
search = x.split() | |
search = '+'.join(map(str, search)) | |
url = 'https://www.google.nl/search?as_st=y&tbm=isch&hl=nl&as_q=%s&as_epq=&as_oq=&as_eq=&cr=&as_sitesearch=&safe=active&tbs=iar:s' % search | |
request = urllib2.Request( url, headers=request_headers) | |
resp = urllib2.urlopen(request).read() | |
soup = BeautifulSoup(resp,"html.parser") | |
links = soup.find_all('div',attrs={"class" : "rg_meta notranslate"}) | |
image = json.loads(links[0].text) | |
return image['ou'] | |
def google_video(x): | |
search = x.split() | |
search = '+'.join(map(str, search)) | |
url = 'https://www.google.nl/search?q=%s&tbm=vid&source=lnms&sa=X&ved=0ahUKEwijn4nG8YTYAhUBzaQKHUyRBBkQ_AUICygC&biw=1348&bih=643&dpr=1' % search | |
request = urllib2.Request( url, headers=request_headers) | |
resp = urllib2.urlopen(request).read() | |
soup = BeautifulSoup(resp,"html.parser") | |
links = soup.find_all('h3',attrs={"class" : "r"}) | |
videos = [] | |
for h3 in links: | |
for link in h3.find_all('a'): | |
videos.append( link.get('href') ) | |
return videos[0] | |
print google_video("beasty boys") | |
print google_image("beasty boys") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment