-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
308 lines (246 loc) · 10.6 KB
/
Copy pathscript.py
File metadata and controls
308 lines (246 loc) · 10.6 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
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
import os
import json
import time
import logging
from typing import Dict, Any, Optional
import requests
from bs4 import BeautifulSoup
from markdownify import markdownify as md
from openai import OpenAI # Yeni import yöntemi
# Logging yapılandırması
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
logger = logging.getLogger("roma_yemek_scraper")
class RomaYemekScraper:
def __init__(self,
openai_api_key: str,
proxy: Optional[str] = None,
user_agent: Optional[str] = None,
timeout: int = 30):
"""
Roma yemek rehberi için web scraping ve OpenAI API entegrasyonu.
Args:
openai_api_key: OpenAI API anahtarı
proxy: Opsiyonel proxy URL'si (format: "http://user:pass@host:port")
user_agent: Opsiyonel user agent string
timeout: İstek zaman aşımı (saniye)
"""
self.openai_api_key = openai_api_key
self.proxy = proxy
self.user_agent = user_agent or "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
self.timeout = timeout
# OpenAI API yapılandırması (yeni yöntem)
self.client = OpenAI(api_key=self.openai_api_key)
logger.info("Roma Yemek Scraper başlatıldı")
def scrape_with_requests(self, url: str) -> str:
"""
Requests kullanarak web sayfasını yükler ve içeriği çeker.
Args:
url: Scrape edilecek URL
Returns:
Çekilen HTML içeriği
"""
logger.info(f"Requests ile scraping başlatılıyor: {url}")
headers = {
'User-Agent': self.user_agent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'tr,en-US;q=0.7,en;q=0.3',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Cache-Control': 'max-age=0',
}
proxies = None
if self.proxy:
proxies = {
'http': self.proxy,
'https': self.proxy
}
try:
response = requests.get(
url,
headers=headers,
proxies=proxies,
timeout=self.timeout
)
response.raise_for_status() # HTTP hataları için istisna fırlat
logger.info(f"Sayfa başarıyla yüklendi: {url}")
return response.text
except Exception as e:
logger.error(f"Scraping hatası: {str(e)}")
return ""
def html_to_markdown(self, html_content: str) -> str:
"""
HTML içeriğini Markdown formatına dönüştürür.
Args:
html_content: HTML içeriği
Returns:
Markdown formatındaki içerik
"""
soup = BeautifulSoup(html_content, 'html.parser')
# Gereksiz elementleri temizle
for element in soup.select('script, style, iframe, nav, footer, header, aside, .site-header, .site-footer, .menu-toggle, .search-form'):
if element:
element.decompose()
# Ana içeriği al - Roma yemek rehberi sayfasında article içinde
content = soup.select_one("article")
if not content:
logger.warning("Article seçicisiyle içerik bulunamadı, tüm body kullanılıyor")
content = soup.body or soup
# Markdown'a dönüştür
markdown_content = md(str(content), heading_style="ATX")
# Markdown'ı temizle ve düzenle
markdown_content = self._clean_markdown(markdown_content)
logger.info(f"HTML içeriği Markdown'a dönüştürüldü ({len(markdown_content)} karakter)")
return markdown_content
def _clean_markdown(self, markdown_content: str) -> str:
"""
Markdown içeriğini temizler ve düzenler.
Args:
markdown_content: Temizlenecek Markdown içeriği
Returns:
Temizlenmiş Markdown içeriği
"""
# Fazla boş satırları temizle
import re
markdown_content = re.sub(r'\n{3,}', '\n\n', markdown_content)
# Başlangıç ve sondaki boşlukları temizle
markdown_content = markdown_content.strip()
# Gereksiz metinleri temizle
markdown_content = re.sub(r'Skip to content.*?Biz Evde Yokuz', 'Biz Evde Yokuz', markdown_content, flags=re.DOTALL)
return markdown_content
def process_with_openai(self, markdown_content: str) -> Dict[str, Any]:
"""
Markdown içeriğini OpenAI API ile işler.
Args:
markdown_content: İşlenecek Markdown içeriği
Returns:
OpenAI API'den dönen yanıt
"""
logger.info("OpenAI API ile içerik işleniyor")
# İçerik çok uzunsa kısalt
if len(markdown_content) > 15000: # GPT-3.5 için daha kısa tutuyoruz
logger.warning("İçerik çok uzun, kısaltılıyor")
markdown_content = markdown_content[:15000] + "\n\n[İçerik çok uzun olduğu için kısaltıldı]"
prompt = """
Bu içerik Roma'daki yemek rehberi hakkında bilgiler içeriyor. Lütfen aşağıdaki bilgileri çıkar ve JSON formatında yanıt ver:
1. Roma'da denenmesi gereken en önemli yemekler ve tatlılar nelerdir?
2. Her yemek için en iyi restoranlar hangileridir? (Adres ve iletişim bilgileriyle)
3. Roma mutfağının genel özellikleri nelerdir?
Yanıtını şu formatta yapılandır:
{
"roma_mutfagi_ozellikleri": "...",
"yemekler": [
{
"isim": "Yemek adı",
"aciklama": "Yemek hakkında kısa açıklama",
"en_iyi_restoranlar": [
{
"isim": "Restoran adı",
"adres": "Adres",
"iletisim": "Telefon veya web sitesi"
}
]
}
]
}
İşlenecek içerik:
"""
prompt += markdown_content
try:
# OpenAI API çağrısı (yeni yöntem)
response = self.client.chat.completions.create(
model="gpt-3.5-turbo-16k", # Daha uzun içerik için 16k modeli
messages=[
{"role": "system", "content": "Sen bir yemek ve seyahat uzmanısın. Verilen içerikten yapılandırılmış bilgi çıkarabilirsin."},
{"role": "user", "content": prompt}
],
temperature=0.2,
max_tokens=4000,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0
)
# Yanıtı JSON formatına çevir
try:
# Yanıt metnini al (yeni yöntem)
result_text = response.choices[0].message.content
# JSON bloğunu çıkar (eğer varsa)
import re
json_match = re.search(r'```json\s*([\s\S]*?)\s*```', result_text)
if json_match:
result_text = json_match.group(1)
result = json.loads(result_text)
logger.info("OpenAI API yanıtı başarıyla JSON'a dönüştürüldü")
except json.JSONDecodeError:
# JSON formatında değilse, metin olarak döndür
logger.warning("OpenAI API yanıtı JSON formatında değil, metin olarak döndürülüyor")
result = {
"error": False,
"text_response": result_text,
"format": "text"
}
return result
except Exception as e:
logger.error(f"OpenAI API hatası: {str(e)}")
return {
"error": True,
"error_message": str(e)
}
def scrape_and_process_roma_yemek(self, url: str) -> Dict[str, Any]:
"""
Roma yemek rehberi sayfasını scrape eder ve OpenAI API ile işler.
Args:
url: Roma yemek rehberi URL'si
Returns:
İşlenmiş sonuçları içeren sözlük
"""
results = {
"url": url,
"title": "Roma'da Ne Nerede Yenir",
"processed": False,
"openai_result": {}
}
logger.info(f"Roma yemek rehberi scraping başlatılıyor: {url}")
# Sayfa içeriğini çek
html_content = self.scrape_with_requests(url)
if not html_content:
logger.error(f"İçerik çekilemedi: {url}")
return results
# Markdown'a dönüştür
markdown_content = self.html_to_markdown(html_content)
# OpenAI API ile işle
openai_result = self.process_with_openai(markdown_content)
# Sonuçları kaydet
results["openai_result"] = openai_result
results["processed"] = True
logger.info("Roma yemek rehberi işlendi.")
return results
def main():
"""
Ana fonksiyon - Roma yemek rehberi scraper'ı çalıştırır.
"""
# OpenAI API anahtarı
openai_api_key = "open_api_keyiniz"
# Roma yemek rehberi URL'si
roma_yemek_url = "https://www.bizevdeyokuz.com/roma-ne-nerede-yenir"
# Scraper'ı oluştur
scraper = RomaYemekScraper(openai_api_key=openai_api_key)
# Scraping ve işleme
results = scraper.scrape_and_process_roma_yemek(roma_yemek_url)
# Sonuçları kaydet
with open("roma_yemek_rehberi.json", 'w', encoding='utf-8') as f:
json.dump(results, f, ensure_ascii=False, indent=2)
logger.info("Sonuçlar kaydedildi: roma_yemek_rehberi.json")
# Sonuçları ekrana yazdır
if results["processed"] and "error" not in results["openai_result"]:
print("\n\n=== ROMA YEMEK REHBERİ ÖZETİ ===\n")
print(json.dumps(results["openai_result"], ensure_ascii=False, indent=2))
else:
print("\n\nHata: Roma yemek rehberi işlenemedi.")
if "error_message" in results["openai_result"]:
print(f"Hata mesajı: {results['openai_result']['error_message']}")
if __name__ == "__main__":
main()