-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathget_gif_9gag.py
51 lines (41 loc) · 1.33 KB
/
get_gif_9gag.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
def get_home_page():
# get articles
try:
body = requests.get("http://9gag.com/gif")
if body.status_code == 200:
html = BeautifulSoup(body.text, 'html.parser')
articles = html.find_all('article')
return articles
except:
print('Error!')
return False
def parse_data(original_data):
list_parsed_data = []
for value in original_data:
object_save = {
'name': '',
'slug': '',
'webm': '',
'mp4': '',
'image': '',
}
html = BeautifulSoup(str(value), 'html.parser')
try:
object_save['name'] = html.find(
'h2', {'class': 'badge-item-title'}).text.strip()
object_save['img'] = html.find(
'img', {'class': 'badge-item-img'}).attrs['src'].strip()
sources = html.find_all('source')
for source in sources:
src = source.attrs['src']
if 'mp4' in src:
object_save['mp4'] = src
if 'webm' in src:
object_save['webm'] = src
list_parsed_data.append(source)
except:
print('Data error!')
return list_parsed_data
print(parse_data(get_home_page()))