File size: 6,042 Bytes
f7737bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68ca1f1
f7737bb
 
 
 
 
 
 
68ca1f1
f7737bb
 
 
 
 
 
 
 
68ca1f1
f7737bb
 
 
 
 
 
 
 
 
 
 
68ca1f1
f7737bb
 
 
 
 
 
2f35e5f
 
 
 
 
f7737bb
7225716
2f35e5f
 
 
 
 
f7737bb
68ca1f1
2f35e5f
 
 
 
 
 
f7737bb
7225716
2f35e5f
 
 
 
 
68ca1f1
 
2f35e5f
 
 
 
 
 
68ca1f1
2f35e5f
f7737bb
 
 
2f35e5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7737bb
2f35e5f
 
f7737bb
 
2f35e5f
 
 
 
f7737bb
2f35e5f
 
 
f7737bb
 
 
2f35e5f
f7737bb
2f35e5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7737bb
2f35e5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7737bb
 
2f35e5f
f7737bb
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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>HTML Compressor for LLM</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/plugins/toolbar/prism-toolbar.min.css">
  <style>
    :root {
      --primary-color: #007bff;
      --secondary-color: #6c757d;
      --success-color: #28a745;
      --border-color: #dee2e6;
      --background-color: #f8f9fa;
    }

    body {
      font-family: system-ui, -apple-system, sans-serif;
      line-height: 1.6;
      margin: 0;
      padding: 20px;
      background: var(--background-color);
    }

    .container {
      max-width: 1200px;
      margin: 0 auto;
      background: white;
      padding: 30px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    textarea {
      width: 100%;
      height: 200px;
      padding: 12px;
      border: 1px solid var(--border-color);
      border-radius: 4px;
      font-family: 'Monaco', 'Menlo', monospace;
      font-size: 14px;
      resize: vertical;
      margin-bottom: 15px;
    }

    .options-container {
      background: var(--background-color);
      padding: 20px;
      border-radius: 8px;
      margin: 20px 0;
    }
    .input-section {
        margin-bottom: 30px;
        padding: 20px;
        border: 1px solid var(--border-color);
        border-radius: 8px;
    }

    .script-section {
        margin: 20px 0;
        padding: 20px;
        background: var(--background-color);
        border-radius: 8px;
    }

    #userInput {
        width: 100%;
        padding: 12px;
        margin-bottom: 15px;
        border: 1px solid var(--border-color);
        border-radius: 4px;
    }

    #scriptOutput {
        background: white;
        padding: 15px;
        border-radius: 4px;
        margin-top: 15px;
    }

    .extraction-results {
        margin-top: 20px;
        padding: 15px;
        background: white;
        border-radius: 4px;
        border: 1px solid var(--border-color);
    }
    </style>
</head>
<body>
    <div class="container">
        <h1>HTML Data Extractor</h1>
        
        <div class="input-section">
            <h2>Input HTML</h2>
            <textarea 
                id="htmlInput" 
                placeholder="Paste your HTML here..."
            ></textarea>

            <h3>Extraction Query</h3>
            <input 
                type="text" 
                id="userInput" 
                placeholder="Describe what data you want to extract..."
            >
            
            <div class="button-group">
                <button id="generateScript">Generate Script</button>
                <button id="extractData" disabled>Extract Data</button>
            </div>
        </div>

        <div class="script-section" style="display: none;">
            <h2>Generated Cheerio Script</h2>
            <pre><code class="language-javascript" id="scriptOutput"></code></pre>
        </div>

        <div class="extraction-results" style="display: none;">
            <h2>Extracted Data</h2>
            <pre><code class="language-json" id="extractionOutput"></code></pre>
        </div>
    </div>

    <script>
        const generateScriptBtn = document.getElementById('generateScript');
        const extractDataBtn = document.getElementById('extractData');
        const htmlInput = document.getElementById('htmlInput');
        const userInput = document.getElementById('userInput');
        const scriptSection = document.querySelector('.script-section');
        const extractionResults = document.querySelector('.extraction-results');

        let currentScript = '';

        generateScriptBtn.addEventListener('click', async () => {
            try {
                const response = await fetch('https://elevatics-ai-web-scraper-chat.hf.space/api/v1/generate-cheerio-script', {
                    method: 'POST',
                    headers: {
                        'accept': 'application/json',
                        'x-api-key': 'ae54a922-ed3a-4634-be4a-4e4dd470800a',
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        html: htmlInput.value,
                        user_input: userInput.value
                    })
                });

                const data = await response.json();
                
                if (data.status === 'success') {
                    currentScript = data.cheerio_script;
                    document.getElementById('scriptOutput').textContent = currentScript;
                    scriptSection.style.display = 'block';
                    extractDataBtn.disabled = false;
                    Prism.highlightAll();
                }
            } catch (error) {
                alert('Error generating script: ' + error.message);
            }
        });

        extractDataBtn.addEventListener('click', async () => {
            try {
                const response = await fetch('/extract', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        html: htmlInput.value,
                        script: currentScript
                    })
                });

                const data = await response.json();
                
                if (data.success) {
                    document.getElementById('extractionOutput').textContent = 
                        JSON.stringify(data.data, null, 2);
                    extractionResults.style.display = 'block';
                    Prism.highlightAll();
                } else {
                    alert('Extraction failed: ' + data.error);
                }
            } catch (error) {
                alert('Error during extraction: ' + error.message);
            }
        });
    </script>
</body>
</html>