r/learnpython • • 1h ago

How would you approach scraping from Wikipedia using Python?

Hey everyone,

I'm learning Python web scraping and wanted to see how more experienced programmers would approach this task. the task is to scrape all Wikipedia articles of Academy Award winners listed under Wikipedia. Source: https://en.wikipedia.org/wiki/List_of_Academy_Award–winning_films

I'm specifically interested in collecting every film listed on that Wikipedia page and downloading its individual Wikipedia article.

I'd really appreciate seeing how others would implement this from scratch using Python, preferably with requests and BeautifulSoup.

A few things I'm curious about:

  • How would you extract all the film links without missing any?
  • How would you download and save each film's Wikipedia article?
  • How would you handle errors or failed downloads?
  • How would you verify that all articles were successfully collected?

If possible, I'd love to see a complete working Python example rather than just a general explanation. I'm trying to compare different approaches and understand what a reliable solution would look like.

Thanks in advance!

12 Upvotes

14 comments sorted by

12

u/ivovis 1h 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

2

u/IllustratorUnfair330 1h ago

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!

•

u/roywill2 57m ago

Bots and scrapers and robots are killing a lot of websites. The least you can do is give money to wikipedia in compensation.

•

u/SpiritedInflation835 52m ago

Wikipedia has an API to access the articles' contents, without having to visit all the websites.

4

u/odaiwai 1h 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.

•

u/apacheotter 49m ago

Was about to say there’s gotta be a package already.

•

u/ivosaurus 35m ago

If possible, I'd love to see a complete working Python example

We doin' your homework for you now?

•

u/aiuniverse-3p3_here 47m 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

•

u/esaule 7m 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!

0

u/mkhanwrites_ 1h 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.

2

u/IllustratorUnfair330 1h ago

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?

•

u/mkhanwrites_ 59m ago edited 54m ago

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)).