-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstart_scapping.py
195 lines (166 loc) · 6.02 KB
/
start_scapping.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import requests
from bs4 import BeautifulSoup
from dataclasses import dataclass
from typing import List
import pandas as pd
import os
from datetime import datetime
@dataclass
class ArchivePage:
archiveList: List[str]
nextPageUrl: str
def getArchivePage(url):
print(f"Page = {url}")
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
nextPageUrl = ""
lotto = []
for link in soup.find_all('a', class_='pagination__item--next'):
nextPageUrl = link.get('href')
break
divContent = soup.find(
'div', class_=['box-cell', 'box-cell--lotto', 'content'])
for link in divContent.find_all('a'):
lottoUrl = link.get('href')
if "/lotto/check/" in lottoUrl:
lotto.append(lottoUrl)
return ArchivePage(archiveList=lotto, nextPageUrl=nextPageUrl)
else:
print(f"Failed to retrieve the page. Status code: {response.status_code}")
def getDate(url):
d = url.split("/")
while ("" in d):
d.remove("")
dateStr = d[len(d)-1]
date = dateStr[0:2]
month = dateStr[2:4]
year = str(int(dateStr[4:8])-543)
return f"{year}-{month}-{date}"
def scappingLotto(url):
if url == 'https://news.sanook.com/lotto/check/ผลสลากกินแบ่งรัฐบาลงวดประจำวันที่1สิงหาคม2552/':
# แก้ไข เฉพาะลิงค์นี้ เพราะหน้าเว็ปสนุก ให้ URL มาไม่ถูกต้อง
url = 'https://news.sanook.com/lotto/check/01082552/'
response = requests.get(url)
date = getDate(url)
print(f'{date} = {url}')
row = {
'date': date,
'prize_1st': '',
'prize_pre_3digit': [],
'prize_sub_3digits': [],
'prize_2digits': [],
'nearby_1st': [],
'prize_2nd': [],
'prize_3rd': [],
'prize_4th': [],
'prize_5th': []
}
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
columns = soup.find_all('div', class_='lottocheck__column')
if columns:
if len(columns) == 4:
i = 0
prefix = []
subfix = []
first = ''
last2digit = ''
for col in columns:
for num in col.find_all('strong'):
if i == 0:
first = num.text
elif i == 1:
prefix.append(num.text)
elif i == 2:
subfix.append(num.text)
elif i == 3:
last2digit = num.text
i = i+1
row['prize_1st'] = first
row['prize_pre_3digit'] = prefix
row['prize_sub_3digits'] = subfix
row['prize_2digits'] = last2digit
else:
i = 0
prefix = []
subfix = []
first = ''
last2digit = ''
for col in columns:
for num in col.find_all('strong'):
if i == 0:
first = num.text
elif i == 1:
subfix.append(num.text)
elif i == 2:
last2digit = num.text
i = i+1
row['prize_1st'] = first
row['prize_pre_3digit'] = prefix
row['prize_sub_3digits'] = subfix
row['prize_2digits'] = last2digit
nearby = []
div = soup.find('div', class_='lottocheck__sec--nearby')
if div:
for ele in div.find_all('strong', class_="lotto__number"):
nearby.append(ele.text)
row['nearby_1st'] = nearby
i = 0
sections = soup.find_all('div', class_='lottocheck__sec')
if sections:
for section in sections:
divs = section.find_all('div', class_='lottocheck__box-item')
nums = []
if i == 0:
i = i+1
continue
for div in divs:
for span in div.find_all('span', class_='lotto__number'):
nums.append(span.text)
if i == 1:
row['prize_2nd'] = nums
elif i == 2:
row['prize_3rd'] = nums
elif i == 3:
row['prize_4th'] = nums
elif i == 4:
row['prize_5th'] = nums
i = i+1
return row
else:
print(f"Failed to retrieve the page. Status code: {response.status_code}")
columns = ['date', 'prize_1st', 'prize_pre_3digit',
'prize_sub_3digits', 'prize_2digits', 'nearby_1st', 'prize_2nd', 'prize_3rd', 'prize_4th', 'prize_5th']
# dtypes = {
# 'date': datetime,
# 'prize_1st': str,
# 'prize_pre_3digit': object,
# 'prize_sub_3digits': object,
# 'prize_2digits': object,
# 'nearby_1st': object,
# 'prize_2nd': object,
# 'prize_3rd': object,
# 'prize_4th': object,
# 'prize_5th': object
# }
df = pd.DataFrame(columns=columns)
archive_url = "https://news.sanook.com/lotto/archive/"
header = True
if os.path.exists('lotto.csv'):
os.remove('lotto.csv')
if os.path.exists('lotto.parquet'):
os.remove('lotto.parquet')
while True:
archive = getArchivePage(archive_url)
for page in archive.archiveList:
new_row = scappingLotto(page)
newDf = pd.DataFrame([new_row])
df = pd.concat([df, newDf], ignore_index=True)
newDf.to_csv('lotto.csv', mode='a', index=False, header=header)
header = False
if archive.nextPageUrl == "":
df.to_parquet('lotto.parquet', index=False)
break
else:
archive_url = archive.nextPageUrl