-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolanet.py
96 lines (71 loc) · 2.79 KB
/
bolanet.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
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import requests
from bs4 import BeautifulSoup
import json
class Bolanet:
#output=[]
def scrapbola(self):
sjson = {}
sjson = []
URL = self.URL
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
#print(soup)
results = soup.find("div", {"class": "newslist"})
#pertama atas
atas = results.find("div", {"class": "item_hl"})
judul1 = atas.find('h2').text
gambar1 = atas.find('img')
#desc1 = atas.find('p', attrs={'class' : 'syn'}).getText()
waktu1 = atas.find("div", {"class": "info"})
link1 = waktu1.find('a').get('href')
#fix
judul1 = judul1.replace('\n','')
gambar1 = gambar1['src'].replace('\n','')
waktu1 = waktu1.getText().replace('\n','')
link1 = link1.replace('\n','')
#dict1[0] = {judul1,gambar1,'null',waktu1,link1}
sjson.append({"judul":judul1,"gambar":gambar1,"desc":'null',"waktu":waktu1,"link":link1})
#print("Berita 1")
#print(judul1.replace('\n',''))
#print(gambar1['src'].replace('\n',''))
#print(waktu1.getText().replace('\n',''))
#print(link1.replace('\n',''))
#print(" ")
#sisanya dibawah
count = 1
results2 = results.findAll("div", {"class": "item"})
for results in results2:
judul = results.find("a", {"class": "ntitle"})
gambar = results.find('img')
#desc = atas.find('p', attrs={'class' : 'syn'}).getText()
waktu = results.find("div", {"class": "info"})
link = waktu.find('a').get('href')
#fix
judul = judul.getText().replace('\n','')
gambar = gambar['src'].replace('\n','')
link = link.replace('\n','')
waktu = waktu.getText().replace('\n','')
sjson.append({"judul":judul,"gambar":gambar,"desc":'no desc',"waktu":waktu,"link":link})
count = count+1
#print("Berita " + str(count))
#print(judul.getText().replace('\n',''))
#print(gambar['src'].replace('\n',''))
#print(link.replace('\n',''))
#print(waktu.getText().replace('\n',''))
#print(" ")
if count==10:
break
#print(sjson)
output = json.dumps(sjson)
return output
# init method or constructor
def __init__(self, url):
self.URL = url
bolanet = Bolanet('https://www.bola.net/tag/liverpool/')
output = bolanet.scrapbola()
#print(bolanet.scrapbola())