Home > database >  Extract the gross from each movie BeautifulSoup Python
Extract the gross from each movie BeautifulSoup Python

Time:07-30

I'm trying to scrap the gross of each movie in this list, but can't seem to extract that value.

The objective is to extract only the gross amount of each movie to later make a table with the name of the movie and the gross.

Already extracted the titles, but I'm having a hard time with the Gross

Here is the code I have

from bs4 import BeautifulSoup
import requests
import pandas as pd
url = "https://www.imdb.com/list/ls024149810/"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

I've tried this, but it returns both "Votes" and "Gross"

gross = soup.find_all('span', attrs = {'name':'nv'})
print(gross)

Also tried this, but it still doesn't work

gross = soup.find_all('span', attrs = {'name':'nv'})[1]['data-value']
print(gross)

CodePudding user response:

This might solve your problem, using the step in the list to select every second occurrence.

from bs4 import BeautifulSoup
import requests

url = "https://www.imdb.com/list/ls024149810/"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

gross_values = soup.find_all('span', attrs = {'name':'nv'})[1::2]
for gross_value in gross:
    print(gross_value.get_text())
  • Related