-
Notifications
You must be signed in to change notification settings - Fork 0
/
okezone.py
83 lines (60 loc) · 2.28 KB
/
okezone.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
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
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 8 22:11:56 2021
@author: Harry
"""
import requests
#import cssutils
from bs4 import BeautifulSoup
import json
class Okezone:
#output=[]
def scrapbola2(self):
sjson = {}
sjson = []
URL = self.URL
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
#print(soup)
results = soup.find("div", {"class": "col-md-7 col-sm-7 right"})
count = 0
results2 = results.findAll("div", {"class": "tag-wrapper"})
for results in results2:
judul = results.find("div", {"class": "quotes"})
judul1 = judul.find("a", {"class": "ga_BreakingMore"}).text
try:
gambar = results.find("div", class_="gambar-head")['style']
gambar1 = gambar.split("('", 1)[1].split("')")[0]
except:
gambar1 = 'no image'
link = results.find('a').get('href')
try:
waktu = results.find("div", {"class": "media-time"}).getText()
except:
waktu = 'no time'
try:
desc = results.find('p').getText()
except:
desc = 'no desc'
count = count+1
judul1 = judul1.replace('\n','')
gambar1 = gambar1.replace('\n','')
link = link.replace('\n','')
waktu = waktu[1:].replace('\n','')
desc = desc.replace('\n','')
#print("Berita " + str(count))
#print(judul1.replace('\n',''))
#print(gambar1.replace('\n',''))
#print(link.replace('\n',''))
#print(waktu[1:].replace('\n',''))
#print(desc.replace('\n',''))
#print(" ")
sjson.append({"judul":judul1,"gambar":gambar1,"desc":desc,"waktu":waktu,"link":link})
output = json.dumps(sjson)
return output
# init method or constructor
def __init__(self, url):
self.URL = url
okezone = Okezone('https://www.okezone.com/tag/liverpool')
output2 = okezone.scrapbola2()
#print(kompas.scrapbola())