Skip to content
Advertisement

Iterating over table of divs using BeautifulSoup

A div of class="tableBody" has many divs as children. I want to get all its div child and get the string which I have highlighted in this picture.

JavaScript

the above code returns me a empty list. enter image description here

I am trying to learn BS4. I appreciate it if you could help me with the code.

Advertisement

Answer

The data you see on the page is loaded dynamically via JavaScript. You can use requests module to simulate it.

For example:

JavaScript

Prints:

JavaScript

EDIT: To get all pages, filter out only ‘Afghanistan’ country and save to CSV, you can use this example:

JavaScript

Saved data.csv (screenshot from LibreOffice):

enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement