【问题标题】:Beautiful Soup IMDB美汤IMDB
【发布时间】:2020-05-12 22:30:54
【问题描述】:

我正在尝试获取 IMDB 顶级电影的名称。但我不知道如何获得具体的电影名称

这里是源代码

<a href="/title/tt0111161/?pf_rd_m=A2FGELUUNOQJNL&amp;pf_rd_p=e31d89dd-322d-4646-8962-327b42fe94b1&amp;pf_rd_r=XCFVE1SQVHJADWSSD8TG&amp;pf_rd_s=center-1&amp;pf_rd_t=15506&amp;pf_rd_i=top&amp;ref_=chttp_tt_1" title="Frank Darabont (dir.), Tim Robbins, Morgan Freeman">The Shawshank Redemption</a>

这是我的代码

import requests
from bs4 import BeautifulSoup as bs

file = open("text-txt-file.txt", "w") 
imdburl1 = "https://www.imdb.com/chart/top"

r = requests.get(imdburl1)

soup = bs(r.content, "lxml")

data = soup.find_all("table", {"class":"chart full-width"})

movietable = (data[0].contents) [len(data[0].contents) - 2]

movietable = movietable.find_all("tr")

for i in movietable:
    filmtitles = i.find_all("td", {"class":"titleColumn"})
    for j in filmtitles:
        moviename = j.find_all("a")
        print() # what to do ????

input()

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup python-requests


    【解决方案1】:

    moviename 上运行循环,然后获取title

        for title in moviename:
            print(title.get('title'))  # what to do ????
    

    完整代码

    import requests
    from bs4 import BeautifulSoup as bs
    
    file = open("text-txt-file.txt", "w")
    imdburl1 = "https://www.imdb.com/chart/top"
    
    r = requests.get(imdburl1)
    
    soup = bs(r.content, "lxml")
    
    data = soup.find_all("table", {"class": "chart full-width"})
    
    movietable = (data[0].contents)[len(data[0].contents) - 2]
    
    movietable = movietable.find_all("tr")
    
    for i in movietable:
        filmtitles = i.find_all("td", {"class": "titleColumn"})
        for j in filmtitles:
            moviename = j.find_all("a")
            for title in moviename:
                print(title.get('title'))  # what to do ????
    

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2013-09-10
      • 2011-04-20
      相关资源
      最近更新 更多