Thanks for the suggestion! I hadn't considered Kiwix. I'm mainly trying to learn how to extract links from a Wikipedia table and download the individual articles using Python and BeautifulSoup, so I'm hoping to understand that approach better. But I'll definitely keep Kiwix in mind!
That was my thought, obviously you are doing it for learning purposes, but they package the whole of Wikipedia for you to download, if you desire. That is by far the easier route.
While I haven't done this, I've heard building a web scraper in Python is fairly easy. I would just look up info on that and apply it to Wikipedia for your use case.
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.
And if you do need scraping, lxml with xpaths is often easier and much faster than bs4 (not to mention selenium, which seems to be the default option for some people). If you want to do less work yourself, Scrapy might be useful.
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.
bro i did same for my project here is simple way use requests + beautifulsoup dont uses selenium for wikipedia its slow
first get the main page find all <a> tags with /wiki/ in it for each links use requests get and save it use try-except, if one fails, skip it and save its name in a failed list at end check len of saved files vs len of links you will know if you missed any
small code: import requests from bs4 import BeautifulSoup url = "https://en.wikipedia.org/wiki/List_of_Academy_Award-winning_films" soup = BeautifulSoup(requests.get(url).text, "html.parser") links = [a['href'] for a in soup.find_all('a', href=True) if '/wiki/' in a['href']]
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.
Thank you for the suggestion! I'm currently using a for loop with BeautifulSoup to extract the film links from the Wikipedia table and then download each article individually. Since all the films are listed on one page, would you say a simple loop is sufficient, or is there a specific advantage to using BFS or DFS in this case?
It sounds to me like you've accomplished what you set out to? Nice work.
You're already using a searching algorithm. The Wikipedia page (hyperlink to the List of Academy Award winning films page) can be thought of as a graph (abstract data type in computer science). The nodes on the graph are the hyperlinks. You were asking basically how to traverse the graph, going down one level from the initial node (the original page) to get to the distance-1 or level-1 nodes (level if it were a tree, which it probably isn't since it's Wikipedia and likely contains cycles).
You could compare your current for-loop code to the pseudocode on the Wikipedia pages for breadth-first search and depth-first search (both graph/tree traversal algorithms) and see which one it is most similar to. Perhaps it's not the full thing but close to it (maybe you only implemented enough code for a 1-level or 1-distance traversal) in which case actually BFS and DFS would look the same (and I guess I'd be wrong about your code being BFS or DFS since you would have implemented a limited solution - perfect for your problem, and not a more generalized one that could be used for an extension of your problem (e.g. download all pages one and two clicks away from the original page)).
40
u/ivovis 2d 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 2d ago
We doin' your homework for you now?
26
u/SpiritedInflation835 2d ago
Wikipedia has an API to access the articles' contents, without having to visit all the websites.
11
u/odaiwai 2d 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 2d 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 2d 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 2d 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 2d ago
Wikipedia has implemented a blocker. It stopped my script to scrape movie plots using BeautifulSoup from working.
2
u/Explorer651 2d 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 2d ago
I wouldn't scrape it. I would just download the entire Wikipedia.
1
u/aiuniverse-3p3_here 2d ago
bro i did same for my project here is simple way use requests + beautifulsoup dont uses selenium for wikipedia its slow
first get the main page find all <a> tags with /wiki/ in it for each links use requests get and save it use try-except, if one fails, skip it and save its name in a failed list at end check len of saved files vs len of links you will know if you missed any
small code: import requests from bs4 import BeautifulSoup url = "https://en.wikipedia.org/wiki/List_of_Academy_Award-winning_films" soup = BeautifulSoup(requests.get(url).text, "html.parser") links = [a['href'] for a in soup.find_all('a', href=True) if '/wiki/' in a['href']]
try this it works
1
u/dariusbiggs 2d 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 2d ago
That's the exact project the Book Webscraping in/with Python uses as it's starting tutorial.
1
u/longgamma 2d ago
Just download the offline version.
1
u/GreenWoodDragon 2d ago
Check if there's an API first. Don't just dive in and scrape the html.
0
u/mkhanwrites_ 2d 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 2d ago
That's a thing to code with ai