-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGEO.py
More file actions
200 lines (168 loc) · 6.22 KB
/
GEO.py
File metadata and controls
200 lines (168 loc) · 6.22 KB
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
196
197
198
199
200
from bs4 import BeautifulSoup as bsp
import hashlib
import logging
import pandas as pd
import requests
import time
from selenium import webdriver
def generateMenu(url) -> list:
try:
response = requests.get(url)
response.raise_for_status()
soup = bsp(response.text, 'lxml')
navbar = soup.find('div', class_='menu-area')
if navbar:
links = [a['href'] for a in navbar.find_all('a', class_='open-section', href=True)]
newLinks = links[2:6] + links[10:12] + links[13:15]
logging.info(f"Menu links found: {newLinks}")
return newLinks
else:
logging.warning("Navbar not found")
return []
except requests.RequestException as e:
logging.error(f"Request failed: {e}")
return []
def generateNews(url) -> list:
try:
response = requests.get(url)
response.raise_for_status()
soup = bsp(response.text, 'lxml')
board = soup.find('div', class_='row video-list laodMoreCatNews')
if board:
news = [a['href'] for a in board.find_all('a', class_='open-section', href=True)]
return news
else:
logging.warning("News board not found")
return []
except requests.RequestException as e:
logging.error(f"Request failed: {e}")
return []
def calculate_hash(title, date, link):
hash_object = hashlib.sha256()
hash_object.update(f"{title}{date}{link}".encode('utf-8'))
return hash_object.hexdigest()
def scrapeData(link, info, stored_hashes):
l = 0
try:
# driver = webdriver.Chrome()
# driver.get(link)
response = requests.get(link)
# # driver = webdriver.Chrome()
# # driver.get(link)
# driver.quit()
response.raise_for_status()
soup = bsp(response.text, 'lxml')
board = soup.find('div', class_='column-right')
receipt = board.find('div', class_='content-area')
try:
title = board.find('div', class_='heading_H').find('h1').text.strip() if board.find('div', class_='heading_H') else 'No Title'
print(title)
except:
title = " "
l +=1
try:
summary = board.find('div', class_='except').text.strip() if board.find('div', class_='except') else 'No Summary'
# print(summary)
except:
summary = " "
l +=1
try:
category = board.find('div', class_='breadcrumb').text.strip() if board.find('div', class_='breadcrumb') else 'No Category'
# print(category)
except:
category = " "
l +=1
try:
date = board.find('p', class_='post-date-time').text.strip() if board.find('p', class_='post-date-time') else 'No Date'
# print(date)
except:
date = " "
l +=1
try:
image = receipt.find('img', src=True)['src'] if receipt.find('img', src=True) else 'No Image'
# print(image)
except:
image = " "
l +=1
try:
paragraphs = [p.text.strip() for p in receipt.find_all('p')] if receipt else []
# print(paragraphs)
except:
paragraphs = " "
l +=1
article_hash = calculate_hash(title, date, link)
if article_hash in stored_hashes:
logging.info(f"Article already stored: {title}")
return
info['Header'].append(title)
info['Summary'].append(summary)
info['Category'].append(category)
info['CreationDate'].append(date)
info['Pic_url'].append(image)
info['Link'].append(link)
info['Detail'].append(paragraphs)
print("\n\n\n\n=======================================================", article_hash ,"=======================================================\n\n\n")
stored_hashes.add(article_hash)
if( l== 7):
with open('junk.csv', 'a') as f:
f.write(receipt.text)
l = 0
except Exception as e:
logging.error(f"An error occurred while scraping data from {link}: {e}")
if( l== 7):
with open('junk.csv', 'a') as f:
f.write(receipt.text)
l = 0
def load_existing_hashes(filename):
hashes = set()
try:
df = pd.read_csv(filename)
for _, row in df.iterrows():
article_hash = calculate_hash(row['Header'], row['CreationDate'], row['Link'])
# print("\n\n\n||||||||||||||||||||||||||||||||||||||||||||||||||||||", article_hash ,"||||||||||||||||||||||||||||||||||||||||||||||||||||||\n\n\n")
hashes.add(article_hash)
except FileNotFoundError:
logging.info(f"{filename} not found. Starting fresh.")
except Exception as e:
logging.error(f"An error occurred while loading existing hashes: {e}")
return hashes
if __name__ == '__main__':
info = {
'Header': [],
'Summary': [],
'Detail': [],
'Link': [],
'Category': [],
'CreationDate': [],
'Pic_url': []
}
base_url = 'https://www.geo.tv/'
navs = generateMenu(base_url)
news_links = []
for nav in navs:
news_links.extend(generateNews(nav))
existing_hashes = load_existing_hashes('GEO.csv')
print(existing_hashes)
for link in news_links:
scrapeData(link, info, existing_hashes)
# Adding sleep to prevent overloading the server
time.sleep(1)
csv_file = 'newGeo.csv'
# Write dictionary to CSV file
new_df = pd.DataFrame(info)
try:
existing_df = pd.read_csv(csv_file, index_col=0)
last_index = existing_df.index[-1]
except FileNotFoundError:
last_index = -1 # If the file doesn't exist, start from index 0
# # Create a new DataFrame to append
# new_data = {
# 'Column1': [10, 20, 30],
# 'Column2': [40, 50, 60]
# }
# new_df = pd.DataFrame(new_data)
# Adjust the index of the new DataFrame
new_df.index += last_index + 1
# Append the new DataFrame to the CSV
new_df.to_csv(csv_file, mode='a', header=False)
print("Data appended successfully with continued index.")