0
from bs4 import BeautifulSoup
import requests 


try:
    source = requests.get('https://www.imdb.com/search/title/?country_of_origin=NP&sort=year,asc')
    source.raise_for_status()

    soup = BeautifulSoup(source.text, 'html.parser')
    #print(soup)
    movies = soup.find('div',class_ ="lister-list").findAll('div', class_= "lister-item mode-advanced")
    print(len(movies))

    for movie in movies:
      
        name = movie.find('h3', class_= "lister-item-header").a.text
        year = movie.find('span', class_ = "lister-item-year text-muted unbold").text.strip('()')
        ratings = movie.find('strong').text
        director = movie.find('p').findAll('a', class_ = "Director:").text

        print(director)
        break
       

except Exception as e:
    print(e)

I need Directors name just like other data.

0stone0
  • 34,288
  • 4
  • 39
  • 64

1 Answers1

0

The directors element doesn't seem to have a unique id or class associated with it.

You could get the second to last <p>, and then search that for the <a>'s:

directorContainers = movie.find_all('p')[-2]
directors = directorContainers.find_all('a')

Output:

[
    <a href="/name/nm4651234/">Hira Singh Khatri</a>,
    <a href="/name/nm4630931/">Shiva Shankar</a>,
    <a href="/name/nm1085102/">Bhubhan Chand</a>,
    <a href="/name/nm4636559/">Bhim Bahadur</a>,
    <a href="/name/nm1513325/">Basundhara Bhusal</a>
]

If you just need the names, use text on the element:

directorNames = [ director.text for director in directors ]
['Hira Singh Khatri', 'Shiva Shankar', 'Bhubhan Chand', 'Bhim Bahadur', 'Basundhara Bhusal']
0stone0
  • 34,288
  • 4
  • 39
  • 64