[Python] Empty dataframe with ust column headers

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to scrape data from the IMDB website with the following lines of code but I got an empty dataframe with just the column headers after importing the file:

    excel = openpyxl.Workbook() # Creating an excel workbook where the data will be saved
    sheet = excel.active
    sheet.title = 'Top Rated Movies' # naming the sheet
    sheet.append(['rank', 'title', 'year', 'rating']) # naming the column headers


    try:
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36'
    }
    source = requests.get('https://www.imdb.com/chart/top/', headers=headers)
    source.raise_for_status()

    soup = BeautifulSoup(source.content, 'html.parser')

    movies = soup.find_all('tr')
    for movie in movies[1:]: # Skip the first row (header)
    cells = movie.find_all('td')
    rank = cells[0].text.strip()
    title = cells[1].find('a').text.strip()
    year = cells[1].find('span', class_='secondaryInfo').text.strip('()')
    rating = cells[2].text.strip()

    print(f"Rank: {rank}, Title: {title}, Year: {year}, Rating: {rating}")

    except Exception as e:
    print(e)


    excel.save('IMDB Movie Ratings.xlsx')

    Continue reading...

Compartilhe esta Página