-1

I have an issue while scraping data from web

Below Mentioned I was tried

import requests
from bs4 import BeautifulSoup


# Collect and parse first page
page = requests.get('https://www.jiosaavn.com/album/vijayashanti-birthday-telugu-hits/A05RmafpUiI_')
soup = BeautifulSoup(page.text, 'html.parser')

# Pull all text from the BodyText div
artist_name_list = soup.find(class_='song-wrap')

# Pull text from all instances of <a> tag within BodyText div
artist_name_list_items = artist_name_list.find_all('a')

for artist_name in artist_name_list_items:
    print(artist_name.prettify())

I except the proper output

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

find returns None if it doesn't find an appropriate element. You'll need to check its return value before trying to call find_all on it:

# Pull all text from the BodyText div
artist_name_list = soup.find(class_='song-wrap')

# Check that a song-wrap item was found
if artist_name_list:

    # Pull text from all instances of <a> tag within BodyText div
    artist_name_list_items = artist_name_list.find_all('a')

    for artist_name in artist_name_list_items:
        print(artist_name.prettify())
Mureinik
  • 297,002
  • 52
  • 306
  • 350