美丽汤IMDB

时间:2020-05-12 22:30:54

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

我正在尝试获取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()

1 个答案:

答案 0 :(得分: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 ????

enter image description here