get table wikipedia 1

get table wikipedia

import requests
from bs4 import BeautifulSoup

URL = ""

res = requests.get(URL).text
soup = BeautifulSoup(res,'lxml')
for items in soup.find('table', class_='wikitable').find_all('tr')[1::1]:
    data = items.find_all(['th','td'])
        country = data[0].a.text
        title = data[1].a.text
        name = data[1].a.find_next_sibling().text
    except IndexError:pass

Here is what the above code is Doing:
1. We are using the requests library to get the HTML content of the URL.
2. We are using the BeautifulSoup library to parse the HTML content.
3. We are using the find method to find the table with the class wikitable.
4. We are using the find_all method to find all the rows in the table.
5. We are using the for loop to iterate over the rows.
6. We are using the find_all method to find all the columns in the row.
7. We are using the try-except block to handle the IndexError exception.
8. We are printing the country, title, and name.

Similar Posts