Python taking specific data's from websites

Viewed 43

I am new at python and i'm working on interface.I should take top 250 movies from imdb website.

def clicked(self):
    movie=self.movie_name.text()
    
    url="https://www.imdb.com/chart/top/"
    response=requests.get(url)
    html_content=response.content
    soup=BeautifulSoup(html_content,"html.parser")

    movie_name = soup.find_all("td",{"class":"titleColumn"})
    for i in movie_name:
        i=i.text

        i=i.strip()

        i=i.replace("\n","")

        if (movie == i):
            self.yazialani.setText(i) 

and with this code output is like this: 6. Schindler's List(1993) 7. The Lord of the Rings: The Return of the King(2003) 8. Pulp Fiction(1994) but for my project i just wanna take movies names not years and rankings.How should i change my code?

2 Answers

One primitive solution could be (considering your string is of the tipe digits+. +name_of_movie+(YEAR) is taking just

a=["6. Schindler's List(1993)", "7. The Lord of the Rings: The Return of the King(2003)", "8. Pulp Fiction(1994)"]
just_names=[]
for name in a:
    i=0
    while True:
        if name[i]=='.':
            just_names.append(name[i+2:-6]) # To delete the space after the point
            break
        i+=1

Only the name of the movie is contained in the anchor tag. So select anchor tag text for each td

import requests
from bs4 import BeautifulSoup

url="https://www.imdb.com/chart/top/"
response=requests.get(url)
html_content=response.content
soup=BeautifulSoup(html_content,"html.parser")

movie_name = soup.find_all("td",{"class":"titleColumn"})

for i in movie_name:
    print(i.find("a").get_text(strip=True))

Output:

The Shawshank Redemption
The Godfather
The Godfather: Part II
The Dark Knight
12 Angry Men
Schindler's List
The Lord of the Rings: The Return of the King
Pulp Fiction
Il buono, il brutto, il cattivo
The Lord of the Rings: The Fellowship of the Ring
Fight Club
Forrest Gump
Inception
Star Wars: Episode V - The Empire Strikes Back
The Lord of the Rings: The Two Towers
The Matrix
Goodfellas
One Flew Over the Cuckoo's Nest
Shichinin no samurai
Se7en
La vita è bella
Cidade de Deus
The Silence of the Lambs
Hamilton
It's a Wonderful Life
Star Wars
Saving Private Ryan
Sen to Chihiro no kamikakushi
Gisaengchung
The Green Mile
Interstellar
Léon
The Usual Suspects
Seppuku
The Lion King
Back to the Future
The Pianist
Terminator 2: Judgment Day
American History X
Modern Times
Psycho
Gladiator
City Lights
The Departed
The Intouchables
Whiplash
The Prestige
...
...
..
Related