首页 > 解决方案 > Python 从网站获取特定数据

问题描述

我是 python 新手,我正在研究界面。我应该从 imdb 网站获取前 250 部电影。

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) 

使用此代码输出如下: 6. 辛德勒的名单 (1993) 7. 指环王:王者归来 (2003) 8. 低俗小说 (1994) 但对于我的项目,我只想取电影名称年和排名。我应该如何更改我的代码?

标签: pythonhtmlbeautifulsouprequest

解决方案


一种原始的解决方案可能是(考虑到你的字符串是小费digits+ . + name_of_movie+(YEAR)只是

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

推荐阅读