r/learnpython • • 5d ago
How would you approach scraping from Wikipedia using Python?
[deleted] — view deleted post
4
Upvotes
r/learnpython • • 5d ago
[deleted] — view deleted post
40
u/ivovis 5d ago
A while ago I downloaded the whole of Wikipedia to a usb drive using Kiwix, suspect this would be the best way without hammering actual Wikipeadia
24
u/ivosaurus 5d ago
We doin' your homework for you now?
26
u/SpiritedInflation835 5d ago
Wikipedia has an API to access the articles' contents, without having to visit all the websites.
11
u/odaiwai 5d ago
It's python so a lot of these problems are already solved. Start here: https://pypi.org/project/wikipedia/ and see if it matches your needs. It has links to a few more advanced projects.
16
u/desrtfx but other languages pro 4d ago
If your first instinct is to scrape you're doing it wrong.
First is always to check if there is an API and if so, use it.
Scraping is always the very last resort if there are absolutely no other options.
7
u/esaule 5d ago
Don't! There is much easier
You can hit the wikidata SPARQL endpoint and make a query against the database and get the data that comes back to your formatted.
Much easier to do and maintain!
21
u/roywill2 5d ago
Bots and scrapers and robots are killing a lot of websites. The least you can do is give money to wikipedia in compensation.
3
u/Pirate_OOS 4d ago
Wikipedia has implemented a blocker. It stopped my script to scrape movie plots using BeautifulSoup from working.
2
u/Explorer651 4d ago
Since you're fetching a couple thousand pages, it's worth following the MediaWiki API etiquette. Set a descriptive User-Agent with contact info or you can get IP-blocked, and send requests one at a time instead of in parallel.
3
u/szayl 4d ago
I wouldn't scrape it. I would just download the entire Wikipedia.
1
u/dariusbiggs 4d ago
Clone Wikipedia from its data source (there is probably something for it), then develop and learn against a locally hosted container.
1
u/Umustbecrazy 4d ago
That's the exact project the Book Webscraping in/with Python uses as it's starting tutorial.
1
u/longgamma 4d ago
Just download the offline version.
1
u/GreenWoodDragon 4d ago
Check if there's an API first. Don't just dive in and scrape the html.
0
u/mkhanwrites_ 5d ago
You will probably need some kind of for loop in your code - specifically you'll be using a basic searching algorithm like Breadth-First Search or Depth-First Search. You could also technically implement this recursively. I don't think I'll provide a code sample, sorry about that.
-3
u/vilette 5d ago
That's a thing to code with ai