-
Notifications
You must be signed in to change notification settings - Fork 95
/
scraper.py
400 lines (312 loc) · 15.2 KB
/
scraper.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import os
import random
import time
import re
import json
from datetime import datetime
from typing import List, Dict, Type
import pandas as pd
from bs4 import BeautifulSoup
from pydantic import BaseModel, Field, create_model
import html2text
import tiktoken
import streamlit as st
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from openai import OpenAI
import google.generativeai as genai
from groq import Groq
from api_management import get_api_key
from assets import USER_AGENTS,PRICING,HEADLESS_OPTIONS,SYSTEM_MESSAGE,USER_MESSAGE,LLAMA_MODEL_FULLNAME,GROQ_LLAMA_MODEL_FULLNAME,HEADLESS_OPTIONS_DOCKER
load_dotenv()
# Set up the Chrome WebDriver options
def is_running_in_docker():
"""
Detect if the app is running inside a Docker container.
This checks if the '/proc/1/cgroup' file contains 'docker'.
"""
try:
with open("/proc/1/cgroup", "rt") as file:
return "docker" in file.read()
except Exception:
return False
def setup_selenium(attended_mode=False):
options = Options()
service = Service(ChromeDriverManager().install())
# Apply headless options based on whether the code is running in Docker
if is_running_in_docker():
# Running inside Docker, use Docker-specific headless options
for option in HEADLESS_OPTIONS_DOCKER:
options.add_argument(option)
else:
# Not running inside Docker, use the normal headless options
for option in HEADLESS_OPTIONS:
options.add_argument(option)
# Initialize the WebDriver
driver = webdriver.Chrome(service=service, options=options)
return driver
def fetch_html_selenium(url, attended_mode=False, driver=None):
if driver is None:
driver = setup_selenium(attended_mode)
should_quit = True
if not attended_mode:
driver.get(url)
else:
should_quit = False
# Do not navigate to the URL if in attended mode and driver is already initialized
if not attended_mode:
driver.get(url)
try:
if not attended_mode:
# Add more realistic actions like scrolling
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")
time.sleep(random.uniform(1.1, 1.8))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/1.2);")
time.sleep(random.uniform(1.1, 1.8))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/1);")
time.sleep(random.uniform(1.1, 1.8))
# Get the page source from the current page
html = driver.page_source
return html
finally:
if should_quit:
driver.quit()
def clean_html(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
# Remove headers and footers based on common HTML tags or classes
for element in soup.find_all(['header', 'footer']):
element.decompose() # Remove these tags and their content
return str(soup)
def html_to_markdown_with_readability(html_content):
cleaned_html = clean_html(html_content)
# Convert to markdown
markdown_converter = html2text.HTML2Text()
markdown_converter.ignore_links = False
markdown_content = markdown_converter.handle(cleaned_html)
return markdown_content
def save_raw_data(raw_data: str, output_folder: str, file_name: str):
"""Save raw markdown data to the specified output folder."""
os.makedirs(output_folder, exist_ok=True)
raw_output_path = os.path.join(output_folder, file_name)
with open(raw_output_path, 'w', encoding='utf-8') as f:
f.write(raw_data)
print(f"Raw data saved to {raw_output_path}")
return raw_output_path
def create_dynamic_listing_model(field_names: List[str]) -> Type[BaseModel]:
"""
Dynamically creates a Pydantic model based on provided fields.
field_name is a list of names of the fields to extract from the markdown.
"""
# Create field definitions using aliases for Field parameters
field_definitions = {field: (str, ...) for field in field_names}
# Dynamically create the model with all field
return create_model('DynamicListingModel', **field_definitions)
def create_listings_container_model(listing_model: Type[BaseModel]) -> Type[BaseModel]:
"""
Create a container model that holds a list of the given listing model.
"""
return create_model('DynamicListingsContainer', listings=(List[listing_model], ...))
def trim_to_token_limit(text, model, max_tokens=120000):
encoder = tiktoken.encoding_for_model(model)
tokens = encoder.encode(text)
if len(tokens) > max_tokens:
trimmed_text = encoder.decode(tokens[:max_tokens])
return trimmed_text
return text
def generate_system_message(listing_model: BaseModel) -> str:
"""
Dynamically generate a system message based on the fields in the provided listing model.
"""
# Use the model_json_schema() method to introspect the Pydantic model
schema_info = listing_model.model_json_schema()
# Extract field descriptions from the schema
field_descriptions = []
for field_name, field_info in schema_info["properties"].items():
# Get the field type from the schema info
field_type = field_info["type"]
field_descriptions.append(f'"{field_name}": "{field_type}"')
# Create the JSON schema structure for the listings
schema_structure = ",\n".join(field_descriptions)
# Generate the system message dynamically
system_message = f"""
You are an intelligent text extraction and conversion assistant. Your task is to extract structured information
from the given text and convert it into a pure JSON format. The JSON should contain only the structured data extracted from the text,
with no additional commentary, explanations, or extraneous information.
You could encounter cases where you can't find the data of the fields you have to extract or the data will be in a foreign language.
Please process the following text and provide the output in pure JSON format with no words before or after the JSON:
Please ensure the output strictly follows this schema:
{{
"listings": [
{{
{schema_structure}
}}
]
}} """
return system_message
def format_data(data, DynamicListingsContainer, DynamicListingModel, selected_model):
token_counts = {}
if selected_model in ["gpt-4o-mini", "gpt-4o-2024-08-06"]:
# Use OpenAI API
client = OpenAI(api_key=get_api_key('OPENAI_API_KEY'))
completion = client.beta.chat.completions.parse(
model=selected_model,
messages=[
{"role": "system", "content": SYSTEM_MESSAGE},
{"role": "user", "content": USER_MESSAGE + data},
],
response_format=DynamicListingsContainer
)
# Calculate tokens using tiktoken
encoder = tiktoken.encoding_for_model(selected_model)
input_token_count = len(encoder.encode(USER_MESSAGE + data))
output_token_count = len(encoder.encode(json.dumps(completion.choices[0].message.parsed.dict())))
token_counts = {
"input_tokens": input_token_count,
"output_tokens": output_token_count
}
return completion.choices[0].message.parsed, token_counts
elif selected_model == "gemini-1.5-flash":
# Use Google Gemini API
genai.configure(api_key=get_api_key("GOOGLE_API_KEY"))
model = genai.GenerativeModel('gemini-1.5-flash',
generation_config={
"response_mime_type": "application/json",
"response_schema": DynamicListingsContainer
})
prompt = SYSTEM_MESSAGE + "\n" + USER_MESSAGE + data
# Count input tokens using Gemini's method
input_tokens = model.count_tokens(prompt)
completion = model.generate_content(prompt)
# Extract token counts from usage_metadata
usage_metadata = completion.usage_metadata
token_counts = {
"input_tokens": usage_metadata.prompt_token_count,
"output_tokens": usage_metadata.candidates_token_count
}
return completion.text, token_counts
elif selected_model == "Llama3.1 8B":
# Dynamically generate the system message based on the schema
sys_message = generate_system_message(DynamicListingModel)
# print(SYSTEM_MESSAGE)
# Point to the local server
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
completion = client.chat.completions.create(
model=LLAMA_MODEL_FULLNAME, #change this if needed (use a better model)
messages=[
{"role": "system", "content": sys_message},
{"role": "user", "content": USER_MESSAGE + data}
],
temperature=0.7,
)
# Extract the content from the response
response_content = completion.choices[0].message.content
print(response_content)
# Convert the content from JSON string to a Python dictionary
parsed_response = json.loads(response_content)
# Extract token usage
token_counts = {
"input_tokens": completion.usage.prompt_tokens,
"output_tokens": completion.usage.completion_tokens
}
return parsed_response, token_counts
elif selected_model== "Groq Llama3.1 70b":
# Dynamically generate the system message based on the schema
sys_message = generate_system_message(DynamicListingModel)
# print(SYSTEM_MESSAGE)
# Point to the local server
client = Groq(api_key=get_api_key("GROQ_API_KEY"),)
completion = client.chat.completions.create(
messages=[
{"role": "system","content": sys_message},
{"role": "user","content": USER_MESSAGE + data}
],
model=GROQ_LLAMA_MODEL_FULLNAME,
)
# Extract the content from the response
response_content = completion.choices[0].message.content
# Convert the content from JSON string to a Python dictionary
parsed_response = json.loads(response_content)
# completion.usage
token_counts = {
"input_tokens": completion.usage.prompt_tokens,
"output_tokens": completion.usage.completion_tokens
}
return parsed_response, token_counts
else:
raise ValueError(f"Unsupported model: {selected_model}")
def save_formatted_data(formatted_data, output_folder: str, json_file_name: str, excel_file_name: str):
"""Save formatted data as JSON and Excel in the specified output folder."""
os.makedirs(output_folder, exist_ok=True)
# Parse the formatted data if it's a JSON string (from Gemini API)
if isinstance(formatted_data, str):
try:
formatted_data_dict = json.loads(formatted_data)
except json.JSONDecodeError:
raise ValueError("The provided formatted data is a string but not valid JSON.")
else:
# Handle data from OpenAI or other sources
formatted_data_dict = formatted_data.dict() if hasattr(formatted_data, 'dict') else formatted_data
# Save the formatted data as JSON
json_output_path = os.path.join(output_folder, json_file_name)
with open(json_output_path, 'w', encoding='utf-8') as f:
json.dump(formatted_data_dict, f, indent=4)
print(f"Formatted data saved to JSON at {json_output_path}")
# Prepare data for DataFrame
if isinstance(formatted_data_dict, dict):
# If the data is a dictionary containing lists, assume these lists are records
data_for_df = next(iter(formatted_data_dict.values())) if len(formatted_data_dict) == 1 else formatted_data_dict
elif isinstance(formatted_data_dict, list):
data_for_df = formatted_data_dict
else:
raise ValueError("Formatted data is neither a dictionary nor a list, cannot convert to DataFrame")
# Create DataFrame
try:
df = pd.DataFrame(data_for_df)
print("DataFrame created successfully.")
# Save the DataFrame to an Excel file
excel_output_path = os.path.join(output_folder, excel_file_name)
df.to_excel(excel_output_path, index=False)
print(f"Formatted data saved to Excel at {excel_output_path}")
return df
except Exception as e:
print(f"Error creating DataFrame or saving Excel: {str(e)}")
return None
def calculate_price(token_counts, model):
input_token_count = token_counts.get("input_tokens", 0)
output_token_count = token_counts.get("output_tokens", 0)
# Calculate the costs
input_cost = input_token_count * PRICING[model]["input"]
output_cost = output_token_count * PRICING[model]["output"]
total_cost = input_cost + output_cost
return input_token_count, output_token_count, total_cost
def generate_unique_folder_name(url):
timestamp = datetime.now().strftime('%Y_%m_%d__%H_%M_%S')
url_name = re.sub(r'\W+', '_', url.split('//')[1].split('/')[0]) # Extract domain name and replace non-alphanumeric characters
return f"{url_name}_{timestamp}"
def scrape_url(url: str, fields: List[str], selected_model: str, output_folder: str, file_number: int, markdown: str):
"""Scrape a single URL and save the results."""
try:
# Save raw data
save_raw_data(markdown, output_folder, f'rawData_{file_number}.md')
# Create the dynamic listing model
DynamicListingModel = create_dynamic_listing_model(fields)
# Create the container model that holds a list of the dynamic listing models
DynamicListingsContainer = create_listings_container_model(DynamicListingModel)
# Format data
formatted_data, token_counts = format_data(markdown, DynamicListingsContainer, DynamicListingModel, selected_model)
# Save formatted data
save_formatted_data(formatted_data, output_folder, f'sorted_data_{file_number}.json', f'sorted_data_{file_number}.xlsx')
# Calculate and return token usage and cost
input_tokens, output_tokens, total_cost = calculate_price(token_counts, selected_model)
return input_tokens, output_tokens, total_cost, formatted_data
except Exception as e:
print(f"An error occurred while processing {url}: {e}")
return 0, 0, 0, None
# Remove the main execution block if it's not needed for testing purposes