I am facing a similar problem to the question asked by How can I scrape a list from wikipedia and transfer to a dataframe. I want to create a dataframe from the list 'Modern wars with fewer than 25,000 deaths by death toll' in the Wikipedia page with the column names 'Death toll', 'War', 'Date'.
The solution proposed in the other post does not work for me as the Wikipedia code is different and I can't seem to find a class name for the list.
I am using BeautifulSoup as follows:
url = "https://en.wikipedia.org/wiki/List_of_wars_by_death_toll"
import requests
from bs4 import BeautifulSoup
import pandas as pd
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
I have tried multiple things including searching
soup.find_all('lu')
or soup.find_all("div", {"class": "mw-content-ltr"})[0].find_all("li")
as well as
ul_elements = soup.find_all("ul")
for ul in ul_elements:
# Find all <li> elements within the <ul> element
li_elements = ul.find_all("li")
for li in li_elements:
# Print the text content of each <li> element
print(li.get_text())
Nothing seems to work. The last option for instance prints out way more than alone my list, and I don't know how to limit the result just to my list.
Thanks!