wikipedia_scraper 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +387 -0
- data/Rakefile +8 -0
- data/bin/wikipedia_scraper +11 -0
- data/lib/wikipedia_scraper/page.rb +87 -0
- data/lib/wikipedia_scraper/tag_scraper.rb +269 -0
- data/lib/wikipedia_scraper/version.rb +3 -0
- data/lib/wikipedia_scraper.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 39d22e261aa238cf6caf1b1dc29853a712bde81da50d7bb10ec1ae9a9b602110
|
|
4
|
+
data.tar.gz: 989966ba56260f1106e08944ad2702146018d1ce0d931848a51a0099b26510f9
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: da8e01135625d9251c689b5a8d6438084cc379285d298228cd9d8dbaf107b47c14b5077e8ce4ce4349d277b701e3885e888d53977971d68360a0c4cb3c038c93
|
|
7
|
+
data.tar.gz: daa20c1c153a67f8bb9848e50c3737b6c82b01ad4b16aabdeac241d78b13eb6550e37dfbfd193be602f05204af6ded409b37775ad7377bb2767fd86560bd7dcb
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2011-2026 The Bootstrap Authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
# Wikipedia Scraper
|
|
2
|
+
|
|
3
|
+
This library can be used for scraping Wikipedia articles into a structured JSON format.
|
|
4
|
+
|
|
5
|
+
Unlike traditional scrapers that simply extract plain text, this project preserves the structure of a Wikipedia page by converting it into nested sections, Markdown-formatted text, lists and tables.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Scrape any public Wikipedia article
|
|
10
|
+
- Convert article content into structured JSON
|
|
11
|
+
- Preserve nested sections
|
|
12
|
+
- Convert formatting to Markdown
|
|
13
|
+
- **Bold**
|
|
14
|
+
- *Italic*
|
|
15
|
+
- Links from anchors
|
|
16
|
+
- Extract
|
|
17
|
+
- Paragraphs
|
|
18
|
+
- Ordered & unordered lists
|
|
19
|
+
- References
|
|
20
|
+
- Tables (including rowspan/colspan support)
|
|
21
|
+
- Skip Wikipedia navigation, metadata and styling elements
|
|
22
|
+
- Simple CLI interface
|
|
23
|
+
- Can also be used as a Ruby library
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
Clone the repository:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
git clone https://github.com/rocketdey/wikipedia_scraper.git
|
|
33
|
+
cd wikipedia_scraper
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Install dependencies:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
bundle install
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Gem installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
gem install wikipedia_scraper
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
### Command Line
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
wikipedia_scraper # This will scrape https://en.wikipedia.org/wiki/special:random to current dir
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
wikipedia_scraper https://en.wikipedia.org/wiki/Apple_Inc. ./output/
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This will create
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
output/
|
|
64
|
+
└── Apple_Inc..json
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
### Ruby Library
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
require "wikipedia_scraper"
|
|
73
|
+
|
|
74
|
+
page = WikipediaScraper::Page.fetch(
|
|
75
|
+
"https://en.wikipedia.org/wiki/Alan_Turing"
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
puts page.title
|
|
79
|
+
|
|
80
|
+
page.save_json("./output")
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Example JSON
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"title": "David Chase",
|
|
90
|
+
"short_description": "American writer, director and producer (born 1945)",
|
|
91
|
+
"url": "https://en.wikipedia.org/wiki/David_Chase",
|
|
92
|
+
"sections": [
|
|
93
|
+
{
|
|
94
|
+
"id": 0,
|
|
95
|
+
"heading": null,
|
|
96
|
+
"content": [
|
|
97
|
+
"**David Henry Chase**[1][2] (born August 22, 1945) is an American writer, producer, and director. ..."
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"id": 1,
|
|
102
|
+
"heading": "Early life",
|
|
103
|
+
"content": [
|
|
104
|
+
"Chase was born as an only child to Norma ([née](https://en.wikipedia.org/wiki/Birth_name#Maiden_and_married_names) Bucco) and Enrico \"Henry\" Chase, both born in 1908 and hailing from Italian-American working-class families. ..."
|
|
105
|
+
{
|
|
106
|
+
"id": 2,
|
|
107
|
+
"heading": "Mental health and education",
|
|
108
|
+
"content": [
|
|
109
|
+
"Chase struggled with [panic attacks](https://en.wikipedia.org/wiki/Panic_attacks) and [clinical depression](https://en.wikipedia.org/wiki/Clinical_depression) as a teenager, something that he dealt with into adulthood. ..."
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"id": 3,
|
|
116
|
+
"heading": "Career",
|
|
117
|
+
"content": [
|
|
118
|
+
"Chase started in Hollywood as a [story editor](https://en.wikipedia.org/wiki/Story_editor) for *[Kolchak: The Night Stalker](https://en.wikipedia.org/wiki/Kolchak:_The_Night_Stalker)* and then produced episodes of *[The Rockford Files](https://en.wikipedia.org/wiki/The_Rockford_Files)* and *[Northern Exposure](https://en.wikipedia.org/wiki/Northern_Exposure)*, among other series. ...",
|
|
119
|
+
{
|
|
120
|
+
"id": 4,
|
|
121
|
+
"heading": "*The Sopranos*",
|
|
122
|
+
"content": [
|
|
123
|
+
"Chase worked in relative anonymity before *[The Sopranos](https://en.wikipedia.org/wiki/The_Sopranos)* debuted.[11] The story of *The Sopranos* was initially conceived as a feature film about \"a mobster in therapy having problems with his mother\".[23] Chase got some input from his manager [Lloyd Braun](https://en.wikipedia.org/wiki/Lloyd_Braun_(media_executive)) and decided to adapt it into a television series.[23] ..."
|
|
124
|
+
[
|
|
125
|
+
"*The Sopranos* credits",
|
|
126
|
+
[
|
|
127
|
+
"Writer",
|
|
128
|
+
[
|
|
129
|
+
"\"[The Sopranos](https://en.wikipedia.org/wiki/The_Sopranos_(pilot_episode))\" *(episode 1.01)*",
|
|
130
|
+
"\"[46 Long](https://en.wikipedia.org/wiki/46_Long)\" *(episode 1.02)*",
|
|
131
|
+
"..."
|
|
132
|
+
],
|
|
133
|
+
"Director",
|
|
134
|
+
[
|
|
135
|
+
"\"[The Sopranos](https://en.wikipedia.org/wiki/The_Sopranos_(pilot_episode))\" *(episode 1.01)*",
|
|
136
|
+
"\"[Made in America](https://en.wikipedia.org/wiki/Made_in_America_(The_Sopranos))\" *(episode 6.21)*"
|
|
137
|
+
],
|
|
138
|
+
"Actor",
|
|
139
|
+
"Chase appeared as a man sitting at an outdoor cafe in [Naples](https://en.wikipedia.org/wiki/Naples), Italy smoking a cigarette in the season two episode \"[Commendatori](https://en.wikipedia.org/wiki/Commendatori)\". He also appeared as an airline passenger en route to Italy in season six's \"[Luxury Lounge](https://en.wikipedia.org/wiki/Luxury_Lounge)\". His voice was also used over the phone in the episode \"The Test Dream\"."
|
|
140
|
+
]
|
|
141
|
+
]
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"id": 5,
|
|
146
|
+
"heading": "*Not Fade Away*",
|
|
147
|
+
"content": [
|
|
148
|
+
"*[Not Fade Away](https://en.wikipedia.org/wiki/Not_Fade_Away_(film))* (2012), Chase's feature film debut, was released on December 21, 2012. It centers on the lead singer of a teenage [rock 'n' roll](https://en.wikipedia.org/wiki/Rock_and_roll) band (played by [John Magaro](https://en.wikipedia.org/wiki/John_Magaro)) in 1960s New Jersey.[38][39] ..."
|
|
149
|
+
]
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
"id": 6,
|
|
153
|
+
"heading": "*The Many Saints of Newark*",
|
|
154
|
+
"content": [
|
|
155
|
+
"Although Chase was \"against [the movie] for a long time\",[41] *[Deadline Hollywood](https://en.wikipedia.org/wiki/Deadline_Hollywood)* reported in March 2018 that [New Line Cinema](https://en.wikipedia.org/wiki/New_Line_Cinema) had purchased the script for *[The Many Saints of Newark](https://en.wikipedia.org/wiki/The_Many_Saints_of_Newark)* ..."
|
|
156
|
+
]
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
"id": 7,
|
|
162
|
+
"heading": "Unrealized projects",
|
|
163
|
+
"content": [
|
|
164
|
+
{
|
|
165
|
+
"id": 8,
|
|
166
|
+
"heading": "*A Ribbon of Dreams*",
|
|
167
|
+
"content": [
|
|
168
|
+
"Chase has previously developed *A Ribbon of Dreams*, a [miniseries](https://en.wikipedia.org/wiki/Miniseries) for HBO. According to an HBO [press release](https://en.wikipedia.org/wiki/Press_release), the series' pilot would \"begin in 1913 and follow two men, one a college-educated mechanical engineer, the other a cowboy with a violent past, ..."
|
|
169
|
+
]
|
|
170
|
+
}
|
|
171
|
+
]
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
"id": 9,
|
|
175
|
+
"heading": "Personal life",
|
|
176
|
+
"content": [
|
|
177
|
+
"After graduating from NYU in 1968, Chase moved to California and married his high school sweetheart Denise Kelly.[11] He is the father of actress Michele DeCesare, who appeared in six of *The Sopranos* episodes as [Hunter Scangarelo](https://en.wikipedia.org/wiki/Hunter_Scangarelo).[49]",
|
|
178
|
+
"..."
|
|
179
|
+
]
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
"id": 10,
|
|
183
|
+
"heading": "Select filmography",
|
|
184
|
+
"content": [
|
|
185
|
+
{
|
|
186
|
+
"id": 11,
|
|
187
|
+
"heading": "Television",
|
|
188
|
+
"content": [
|
|
189
|
+
[
|
|
190
|
+
[
|
|
191
|
+
"Year",
|
|
192
|
+
"Title",
|
|
193
|
+
"Director",
|
|
194
|
+
"Writer",
|
|
195
|
+
"Producer",
|
|
196
|
+
"Creator",
|
|
197
|
+
"Notes"
|
|
198
|
+
],
|
|
199
|
+
[
|
|
200
|
+
"1971",
|
|
201
|
+
"*[The Bold Ones: The Lawyers](https://en.wikipedia.org/wiki/The_Bold_Ones:_The_Lawyers)*",
|
|
202
|
+
"No",
|
|
203
|
+
"Yes",
|
|
204
|
+
"No",
|
|
205
|
+
"No",
|
|
206
|
+
"Episode: \"In Defense of Ellen McKay\""
|
|
207
|
+
],
|
|
208
|
+
"..."
|
|
209
|
+
]
|
|
210
|
+
]
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
"id": 12,
|
|
214
|
+
"heading": "Film",
|
|
215
|
+
"content": [
|
|
216
|
+
"..."
|
|
217
|
+
]
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
"id": 13,
|
|
221
|
+
"heading": "Other credits",
|
|
222
|
+
"content": [
|
|
223
|
+
"..."
|
|
224
|
+
]
|
|
225
|
+
}
|
|
226
|
+
]
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
"id": 14,
|
|
230
|
+
"heading": "Awards and recognition",
|
|
231
|
+
"content": [
|
|
232
|
+
"..."
|
|
233
|
+
]
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
"id": 15,
|
|
237
|
+
"heading": "See also",
|
|
238
|
+
"content": [
|
|
239
|
+
"[List of Primetime Emmy Award winners](https://en.wikipedia.org/wiki/List_of_Primetime_Emmy_Award_winners)"
|
|
240
|
+
]
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
"id": 16,
|
|
244
|
+
"heading": "References",
|
|
245
|
+
"content": [
|
|
246
|
+
[
|
|
247
|
+
"1. Chase says his name was not David DeCesare at birth in this interview: [https://interviews.televisionacademy.com/interviews/david-chase#](https://interviews.televisionacademy.com/interviews/david-chase#) [Archived](https://web.archive.org/web/20190331030349/https://interviews.televisionacademy.com/interviews/david-chase)March 31, 2019, at the[Wayback Machine](https://en.wikipedia.org/wiki/Wayback_Machine)",
|
|
248
|
+
"2. Fleming, Mike Jr. (September 7, 2021). [\"David Chase On Reviving 'Sopranos' Spirit With 'The Many Saints Of Newark' And High Interest In Another Prequel Film\"](https://deadline.com/2021/09/david-chase-sopranos-revival-the-many-saints-of-newark-disdain-day-date-bow-interested-in-another-prequel-film-1234828184/). *Deadline*. Retrieved September 8, 2021.",
|
|
249
|
+
"3. *[Wise Guy: David Chase and the Sopranos](https://en.wikipedia.org/wiki/Wise_Guy:_David_Chase_and_the_Sopranos)*",
|
|
250
|
+
"..."
|
|
251
|
+
]
|
|
252
|
+
]
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
"id": 17,
|
|
256
|
+
"heading": "External links",
|
|
257
|
+
"content": [
|
|
258
|
+
[
|
|
259
|
+
"[David Chase](https://www.imdb.com/name/nm0153740/)at[IMDb](https://en.wikipedia.org/wiki/IMDb_(identifier))",
|
|
260
|
+
"[David Chase](https://interviews.televisionacademy.com/interviews/david-chase)at[The Interviews: An Oral History of Television](https://en.wikipedia.org/wiki/The_Interviews:_An_Oral_History_of_Television)"
|
|
261
|
+
]
|
|
262
|
+
]
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Supported Elements
|
|
271
|
+
|
|
272
|
+
| Element | Output |
|
|
273
|
+
|---------|--------|
|
|
274
|
+
| Paragraphs | Markdown text |
|
|
275
|
+
| Links | Markdown links |
|
|
276
|
+
| Bold | `**text**` |
|
|
277
|
+
| Italic | `*text*` |
|
|
278
|
+
| Lists | Ruby Arrays |
|
|
279
|
+
| References | Numbered Arrays |
|
|
280
|
+
| Tables | Nested Arrays |
|
|
281
|
+
| Nested Sections | Recursive Hashes |
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
## Project Structure
|
|
286
|
+
|
|
287
|
+
```
|
|
288
|
+
bin/
|
|
289
|
+
wikipedia_scraper
|
|
290
|
+
|
|
291
|
+
lib/
|
|
292
|
+
wikipedia_scraper/
|
|
293
|
+
page.rb
|
|
294
|
+
tag_scraper.rb
|
|
295
|
+
version.rb
|
|
296
|
+
wikipedia_scraper.rb
|
|
297
|
+
|
|
298
|
+
spec/
|
|
299
|
+
fixtures/
|
|
300
|
+
Chechen_language.html
|
|
301
|
+
Chechen_language.json
|
|
302
|
+
David_Chase.html
|
|
303
|
+
David_Chase.json
|
|
304
|
+
The_Off-Season.html
|
|
305
|
+
The_Off-Season.json
|
|
306
|
+
page_spec.rb
|
|
307
|
+
spec_helper.rb
|
|
308
|
+
tag_scraper_spec.rb
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### `Page`
|
|
312
|
+
|
|
313
|
+
Responsible for
|
|
314
|
+
|
|
315
|
+
- downloading a Wikipedia page
|
|
316
|
+
- parsing the HTML
|
|
317
|
+
- walking through article sections
|
|
318
|
+
- building the final JSON structure
|
|
319
|
+
- saving the result
|
|
320
|
+
|
|
321
|
+
### `TagScraper`
|
|
322
|
+
|
|
323
|
+
Handles HTML parsing and conversion.
|
|
324
|
+
|
|
325
|
+
Responsibilities include:
|
|
326
|
+
|
|
327
|
+
- Markdown conversion
|
|
328
|
+
- List parsing
|
|
329
|
+
- Table parsing
|
|
330
|
+
- Reference extraction
|
|
331
|
+
- Link formatting
|
|
332
|
+
|
|
333
|
+
The module contains no network or file I/O, making it easy to test independently.
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
## Dependencies
|
|
338
|
+
|
|
339
|
+
- HTTParty
|
|
340
|
+
- Nokogiri
|
|
341
|
+
- JSON
|
|
342
|
+
- RSpec (development)
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## Running Tests
|
|
347
|
+
|
|
348
|
+
```bash
|
|
349
|
+
rake spec
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
## Current Limitations
|
|
355
|
+
|
|
356
|
+
- Infobox parsing is currently disabled.
|
|
357
|
+
- Templates and navigation boxes are intentionally ignored.
|
|
358
|
+
- There may be unknown errors since this library is a WIP (Work in Progress).
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
## Future Improvements
|
|
363
|
+
|
|
364
|
+
- Infobox parsing
|
|
365
|
+
- Multi-thread operation
|
|
366
|
+
- Parallel page scraping
|
|
367
|
+
|
|
368
|
+
---
|
|
369
|
+
|
|
370
|
+
## Why this project?
|
|
371
|
+
|
|
372
|
+
The goal of this project is to provide a clean, structured representation of Wikipedia articles suitable for:
|
|
373
|
+
|
|
374
|
+
- LLM datasets
|
|
375
|
+
- Search indexing
|
|
376
|
+
- Knowledge extraction
|
|
377
|
+
- Data analysis
|
|
378
|
+
- Offline archives
|
|
379
|
+
- Markdown generation
|
|
380
|
+
|
|
381
|
+
Rather than scraping raw HTML, the library attempts to preserve the semantic structure of the article.
|
|
382
|
+
|
|
383
|
+
---
|
|
384
|
+
|
|
385
|
+
## License
|
|
386
|
+
|
|
387
|
+
MIT License
|
data/Rakefile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'wikipedia_scraper'
|
|
4
|
+
|
|
5
|
+
url = (!ARGV[0] || ARGV[0] == 'random') ? 'https://en.wikipedia.org/wiki/special:random' : ARGV[0]
|
|
6
|
+
page = WikipediaScraper::Page.fetch(url)
|
|
7
|
+
|
|
8
|
+
output_directory = ARGV.length > 1 ? ARGV[-1] : "." # Change the default output location
|
|
9
|
+
page.save_json(output_directory)
|
|
10
|
+
|
|
11
|
+
puts "Saved #{page.title} to #{output_directory} as #{page.url.split('/').last}.json"
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module WikipediaScraper
|
|
6
|
+
class Page
|
|
7
|
+
HEADERS = {
|
|
8
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0',
|
|
9
|
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
10
|
+
'Accept-Language': 'en-US,en;q=0.9',
|
|
11
|
+
'Connection': 'keep-alive',
|
|
12
|
+
'Upgrade-Insecure-Requests': '1',
|
|
13
|
+
'Sec-Fetch-Dest': 'document',
|
|
14
|
+
'Sec-Fetch-Mode': 'navigate',
|
|
15
|
+
'Sec-Fetch-Site': 'none',
|
|
16
|
+
'Sec-Fetch-User': '?1',
|
|
17
|
+
'Priority': 'u=0, i',
|
|
18
|
+
'TE': 'trailers',
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
attr_reader :url, :title, :short_description, :sections
|
|
22
|
+
|
|
23
|
+
def self.fetch(url)
|
|
24
|
+
response = HTTParty.get(url, headers: HEADERS)
|
|
25
|
+
doc = Nokogiri::HTML(response.body)
|
|
26
|
+
raise "Failed to parse #{url} as HTML" unless doc.html?
|
|
27
|
+
|
|
28
|
+
new(doc)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def initialize(doc)
|
|
32
|
+
@url = doc.at_css("link[rel='canonical']")&.attr('href')
|
|
33
|
+
puts "Scraping #{url}"
|
|
34
|
+
@title = TagScraper.to_markdown(doc.at_css("#firstHeading"))
|
|
35
|
+
@short_description = doc.at_css(".shortdescription")&.text
|
|
36
|
+
@sections = []
|
|
37
|
+
|
|
38
|
+
doc.css(".mw-content-ltr > section").each do |section|
|
|
39
|
+
section_scraper(section, @sections)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def to_h
|
|
44
|
+
{ title: title, short_description: short_description, url: url, sections: sections }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def save_json(path)
|
|
48
|
+
file_path = path[-1] == '/' ? "#{path}#{url.split('/').last}.json" : "#{path}/#{url.split('/').last}.json"
|
|
49
|
+
File.open(file_path, 'w') do |f|
|
|
50
|
+
f.write(JSON.pretty_generate(to_h) + "\n")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def section_scraper(section, parent_content)
|
|
57
|
+
heading_elem = section.at_css("div.mw-heading")
|
|
58
|
+
new_section = {
|
|
59
|
+
id: section['data-mw-section-id'].to_i,
|
|
60
|
+
heading: heading_elem && TagScraper.to_markdown(heading_elem.children[0]),
|
|
61
|
+
content: []
|
|
62
|
+
}
|
|
63
|
+
parent_content << new_section
|
|
64
|
+
|
|
65
|
+
section.children.each do |node|
|
|
66
|
+
if node.name == 'section'
|
|
67
|
+
section_scraper(node, new_section[:content])
|
|
68
|
+
next
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
next unless !node.content.strip.empty? &&
|
|
72
|
+
['p', 'div', 'ol', 'ul', 'table'].include?(node.name) &&
|
|
73
|
+
!node.classes.any? { |c| ['shortdescription', 'metadata', 'hatnote', 'sistersitebox', 'navbox', 'navbox-styles', 'mw-heading'].include?(c) }
|
|
74
|
+
|
|
75
|
+
scraped_node = TagScraper.scrape(node)
|
|
76
|
+
scraped_node = simplify_array(scraped_node)
|
|
77
|
+
next if [nil, "", []].include?(scraped_node)
|
|
78
|
+
new_section[:content] << scraped_node
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def simplify_array(item)
|
|
83
|
+
return item unless item.is_a?(Array)
|
|
84
|
+
item.size == 1 ? simplify_array(item.first) : item.map { |i| simplify_array(i) }
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
module TagScraper
|
|
2
|
+
|
|
3
|
+
def self.scrape(element)
|
|
4
|
+
return nil if element.nil? || element.classes.any? { |c| ['mw-cite-backlink', 'noprint'].include?(c) } || (!element.children.any? && element.text.empty?)
|
|
5
|
+
case element.name
|
|
6
|
+
when "p", "a", "i", "b", "br", "span"
|
|
7
|
+
to_markdown(element)
|
|
8
|
+
when "ol", "ul"
|
|
9
|
+
parse_list(element)
|
|
10
|
+
when "table"
|
|
11
|
+
if !element.classes.include?('wikitable')
|
|
12
|
+
if element.classes.include?('infobox')
|
|
13
|
+
#return parse_infobox(element)
|
|
14
|
+
return nil
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
element_list = element.css(".wikitable, .mw-heading")
|
|
18
|
+
if element_list.empty?
|
|
19
|
+
parse_table([element])
|
|
20
|
+
elsif element_list.any? { |e| e.classes.include?('mw-heading')}
|
|
21
|
+
content_list = []
|
|
22
|
+
if element_list[0].classes.include?('wikitable')
|
|
23
|
+
last_heading = nil
|
|
24
|
+
else
|
|
25
|
+
last_heading = element_list[0].at_css('h2, h3, h4').content
|
|
26
|
+
end
|
|
27
|
+
tables = []
|
|
28
|
+
section = {heading: last_heading, content: tables}
|
|
29
|
+
element_list.drop(1).each do |e|
|
|
30
|
+
if e.classes.include?('mw-heading')
|
|
31
|
+
section = {heading: last_heading, content: tables}
|
|
32
|
+
content_list << section
|
|
33
|
+
last_heading = e.at_css('h2, h3, h4').content
|
|
34
|
+
tables = []
|
|
35
|
+
else
|
|
36
|
+
tables << parse_table([e])
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
content_list
|
|
40
|
+
else
|
|
41
|
+
parse_table(element_list)
|
|
42
|
+
end
|
|
43
|
+
else
|
|
44
|
+
parse_table([element])
|
|
45
|
+
end
|
|
46
|
+
when "div"
|
|
47
|
+
if element.classes.include?('hatnote')
|
|
48
|
+
to_markdown(element)
|
|
49
|
+
else
|
|
50
|
+
result = []
|
|
51
|
+
text_inside_div = ''
|
|
52
|
+
element.children.each do |child|
|
|
53
|
+
if ["p", "a", "i", "b", "br", "span", "text"].include?(child.name)
|
|
54
|
+
text_inside_div << (to_markdown(child) || '')
|
|
55
|
+
else
|
|
56
|
+
result << text_inside_div.strip && text_inside_div = '' unless text_inside_div.strip.empty?
|
|
57
|
+
result << self.scrape(child)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
result << text_inside_div.strip unless text_inside_div.strip.empty?
|
|
61
|
+
result.compact
|
|
62
|
+
end
|
|
63
|
+
when "style", "figure", "sup"
|
|
64
|
+
nil
|
|
65
|
+
else
|
|
66
|
+
if !element.children.empty?
|
|
67
|
+
element.children.map do |child|
|
|
68
|
+
self.scrape(child)
|
|
69
|
+
end.compact
|
|
70
|
+
else
|
|
71
|
+
to_markdown(element)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def self.to_markdown(element)
|
|
77
|
+
|
|
78
|
+
unless element.nil? || element.classes.any? { |c| ['mw-cite-backlink', 'noprint'].include?(c) }
|
|
79
|
+
case element.name
|
|
80
|
+
when 'text'
|
|
81
|
+
content = element.content.gsub(/\s+/, ' ')
|
|
82
|
+
return content.strip.empty? ? nil : content
|
|
83
|
+
when "ol", "ul"
|
|
84
|
+
return parse_list(element).join(', ')
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
inner = element.children.map { |node| markdown_node(node) }.join
|
|
88
|
+
inner = inner.gsub(/\s+/, ' ')
|
|
89
|
+
inner = inner.strip unless inner == ' '
|
|
90
|
+
|
|
91
|
+
case element.name
|
|
92
|
+
when "b"
|
|
93
|
+
"**#{inner}**"
|
|
94
|
+
when "a"
|
|
95
|
+
parse_anchor(element, inner)
|
|
96
|
+
when "i"
|
|
97
|
+
"*#{inner}*"
|
|
98
|
+
else
|
|
99
|
+
inner
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def self.markdown_node(node)
|
|
105
|
+
|
|
106
|
+
return '' if node.classes.any? { |c| ['mw-cite-backlink', 'noprint'].include?(c) }
|
|
107
|
+
|
|
108
|
+
case node.name
|
|
109
|
+
when "text"
|
|
110
|
+
node.content
|
|
111
|
+
when "br"
|
|
112
|
+
' '
|
|
113
|
+
when "span"
|
|
114
|
+
to_markdown(node)
|
|
115
|
+
when "sup"
|
|
116
|
+
return nil if node['style'] == "display:none;"
|
|
117
|
+
node.at_css('a').nil? ? to_markdown(node) : parse_anchor(node.at_css('a'))
|
|
118
|
+
when "style"
|
|
119
|
+
nil
|
|
120
|
+
else
|
|
121
|
+
to_markdown(node)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def self.parse_anchor(element, inner = nil)
|
|
126
|
+
return nil if ['Wikipedia:Citation needed', 'Edit this at Wikidata'].include?(element['title'])
|
|
127
|
+
|
|
128
|
+
url = element['href']
|
|
129
|
+
if !inner.nil? && !inner.strip.empty?
|
|
130
|
+
url_text = inner
|
|
131
|
+
elsif element.content.empty?
|
|
132
|
+
url_text = element['title']
|
|
133
|
+
else
|
|
134
|
+
url_text = element.content
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
if img = element.at_css('img')
|
|
138
|
+
if img['alt'] || img['title']
|
|
139
|
+
url_text = img['alt'] || img['title']
|
|
140
|
+
else
|
|
141
|
+
return '' if img['src']&.include?('/thumb/')
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
if url.include?('&action=edit')
|
|
146
|
+
"https://en.wikipedia.org#{url[18..]}"
|
|
147
|
+
elsif url.include?('#cite_note')
|
|
148
|
+
element.content
|
|
149
|
+
elsif !url_text.nil? && !url_text.empty?
|
|
150
|
+
url.include?('://') ? "[#{url_text}](#{url})".strip : "[#{url_text}](https://en.wikipedia.org#{url[18..]})".strip
|
|
151
|
+
else
|
|
152
|
+
url.include?('://') ? url : "https://en.wikipedia.org#{url[18..]}"
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def self.parse_list(element)
|
|
157
|
+
items = []
|
|
158
|
+
element.css('> li').each do |node|
|
|
159
|
+
if reference_number = node['data-mw-footnote-number']
|
|
160
|
+
items << "#{reference_number}. #{to_markdown(node)}"
|
|
161
|
+
elsif node.matches?('.gallerybox')
|
|
162
|
+
img_node = node.at_css('img')
|
|
163
|
+
url = img_node.parent['href']
|
|
164
|
+
if node.content.empty?
|
|
165
|
+
items << [url.include?('://') ? url : "https://en.wikipedia.org#{url[18..]}", img_node['alt']]
|
|
166
|
+
else
|
|
167
|
+
items << [url.include?('://') ? url : "https://en.wikipedia.org#{url[18..]}", to_markdown(node.at_css(".gallerytext"))]
|
|
168
|
+
end
|
|
169
|
+
else
|
|
170
|
+
items << to_markdown(node)
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
items
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def self.parse_table(element_list)
|
|
178
|
+
main_tables = []
|
|
179
|
+
element_list.each do |element|
|
|
180
|
+
rows = element.css('tr')
|
|
181
|
+
table_caption = to_markdown(element.at_css('caption'))
|
|
182
|
+
table_caption = nil if table_caption&.empty?
|
|
183
|
+
element.css('style').each(&:remove)
|
|
184
|
+
col_length = rows[0].css('th:not([style*="display:none"])', 'td:not([style*="display:none"])').sum { |c| c['colspan'].nil? ? 1 : c['colspan'].to_i}
|
|
185
|
+
table_data = Array.new(rows.length) { Array.new(col_length, nil) }
|
|
186
|
+
rows.each_with_index do |row_element, row_index|
|
|
187
|
+
unless row_element.css('table').empty?
|
|
188
|
+
table_data = parse_table(row_element.css('table'))
|
|
189
|
+
break
|
|
190
|
+
end
|
|
191
|
+
row = row_element.css('th:not([style*="display:none"])', 'td:not([style*="display:none"])')
|
|
192
|
+
cursor_index = col_index = 0
|
|
193
|
+
while col_index < row.length
|
|
194
|
+
data = row[col_index]
|
|
195
|
+
data_content = to_markdown(data)
|
|
196
|
+
if data_content.is_a?(Array)
|
|
197
|
+
table_data[row_index][cursor_index] << data_content
|
|
198
|
+
elsif !table_data[row_index][cursor_index].nil?
|
|
199
|
+
col_index += 1 if data_content.nil? || data_content.empty?
|
|
200
|
+
cursor_index += 1
|
|
201
|
+
break if cursor_index > col_length
|
|
202
|
+
next
|
|
203
|
+
else
|
|
204
|
+
colspan = row[col_index]['colspan'].nil? ? 1 : row[col_index]['colspan'].to_i
|
|
205
|
+
rowspan = row[col_index]['rowspan'].nil? ? 1 : row[col_index]['rowspan'].to_i
|
|
206
|
+
(0..colspan - 1).each do |c|
|
|
207
|
+
(0..rowspan - 1).each do |r|
|
|
208
|
+
table_data[r + row_index][c + cursor_index] = data_content
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
cursor_index += colspan
|
|
213
|
+
col_index += 1
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
table_data.each do |row|
|
|
217
|
+
row.map! { |a| a == '' ? nil : a} if row.is_a?(Array)
|
|
218
|
+
end
|
|
219
|
+
table_data.reject! { |a| a.all?(nil) || a.empty? }
|
|
220
|
+
table_caption.nil? ? main_tables << table_data : main_tables << [table_caption] + table_data
|
|
221
|
+
end
|
|
222
|
+
main_tables.length > 1 ? main_tables : main_tables[0]
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def self.parse_infobox(element)
|
|
226
|
+
main_tables = []
|
|
227
|
+
rows = element.css('tr')
|
|
228
|
+
|
|
229
|
+
rows.each do |row|
|
|
230
|
+
table_data = []
|
|
231
|
+
label = row.at_css("th.infobox-label")
|
|
232
|
+
if label
|
|
233
|
+
row_dict = {label: to_markdown(label), content: []}
|
|
234
|
+
combined_text = ''
|
|
235
|
+
row.at_css('td').children.each do |row_elem|
|
|
236
|
+
case row_elem.name
|
|
237
|
+
when "p", "a", "i", "b", "br", "span", "text"
|
|
238
|
+
combined_text << scrape(row_elem)
|
|
239
|
+
else
|
|
240
|
+
unless combined_text.empty?
|
|
241
|
+
row_dict[:content][-1].nil? ? row_dict[:content] << combined_text : row_dict[:content][-1] << combined_text
|
|
242
|
+
end
|
|
243
|
+
combined_text = ''
|
|
244
|
+
row_dict[:content].push(*scrape(row_elem))
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
main_tables << row_dict
|
|
248
|
+
else
|
|
249
|
+
combined_text = ''
|
|
250
|
+
row.at_css('td').children.each do |row_elem|
|
|
251
|
+
puts combined_text
|
|
252
|
+
case row_elem.name
|
|
253
|
+
when "p", "a", "i", "b", "br", "span", "text"
|
|
254
|
+
combined_text << scrape(row_elem)
|
|
255
|
+
else
|
|
256
|
+
unless combined_text.empty?
|
|
257
|
+
table_data[-1].nil? ? table_data << combined_text : table_data[-1] << combined_text
|
|
258
|
+
end
|
|
259
|
+
combined_text = ''
|
|
260
|
+
data = scrape(row_elem)
|
|
261
|
+
data.is_a?(Array) ? table_data.push(*data) : table_data << data
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
main_tables << table_data
|
|
266
|
+
end
|
|
267
|
+
{id: nil, heading: 'Infobox', title: to_markdown(element.at_css('caption')), content: main_tables}
|
|
268
|
+
end
|
|
269
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: wikipedia_scraper
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- rocketdey
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: httparty
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.21'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.21'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: nokogiri
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.15'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.15'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rspec
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.12'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '3.12'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: rake
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '13.0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '13.0'
|
|
68
|
+
description: Scrape Wikipedia articles into structured JSON, preserving nested sections,
|
|
69
|
+
Markdown-formatted text, lists, tables, and references. Ideal for LLM datasets,
|
|
70
|
+
search indexing, and knowledge extraction.
|
|
71
|
+
email:
|
|
72
|
+
- kursataydin165@gmail.com
|
|
73
|
+
executables:
|
|
74
|
+
- wikipedia_scraper
|
|
75
|
+
extensions: []
|
|
76
|
+
extra_rdoc_files: []
|
|
77
|
+
files:
|
|
78
|
+
- ".gitignore"
|
|
79
|
+
- Gemfile
|
|
80
|
+
- LICENSE.txt
|
|
81
|
+
- README.md
|
|
82
|
+
- Rakefile
|
|
83
|
+
- bin/wikipedia_scraper
|
|
84
|
+
- lib/wikipedia_scraper.rb
|
|
85
|
+
- lib/wikipedia_scraper/page.rb
|
|
86
|
+
- lib/wikipedia_scraper/tag_scraper.rb
|
|
87
|
+
- lib/wikipedia_scraper/version.rb
|
|
88
|
+
homepage: https://github.com/rocketdey/wikipedia_scraper
|
|
89
|
+
licenses:
|
|
90
|
+
- MIT
|
|
91
|
+
metadata:
|
|
92
|
+
homepage_uri: https://github.com/rocketdey/wikipedia_scraper
|
|
93
|
+
source_code_uri: https://github.com/rocketdey/wikipedia_scraper
|
|
94
|
+
changelog_uri: https://github.com/rocketdey/wikipedia_scraper/blob/main/CHANGELOG.md
|
|
95
|
+
rdoc_options: []
|
|
96
|
+
require_paths:
|
|
97
|
+
- lib
|
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: 3.0.0
|
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - ">="
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '0'
|
|
108
|
+
requirements: []
|
|
109
|
+
rubygems_version: 4.0.20
|
|
110
|
+
specification_version: 4
|
|
111
|
+
summary: Scrapes Wikipedia articles into structured markdown/JSON.
|
|
112
|
+
test_files: []
|