所以,現在,我想做的是我試圖從 rottentomatoes.com 上抓取一張桌子,但是每次我運行代碼時,我都會遇到一個問題,它只是列印 <a href 標簽。現在,我想要的只是編號的電影標題。
到目前為止,這是我的代碼:
from requests import get
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
url = "https://www.rottentomatoes.com/top/bestofrt/"
headers = {"Accept-Language": "en-US, en;q=0.5"}
titles = []
year_released = []
def get_requests():
try:
result = requests.get(url=url)
soup = BeautifulSoup(result.text, 'html.parser')
table = soup.find('table', class_='table')
for name in table:
td = soup.find_all('a', class_='unstyled articleLink')
titles.append(td)
print(titles)
break
except:
print("The result could not get fetched")
這是我的輸出:
[[本周開幕,頂級票房,即將上映,周末收入,認證新鮮電影,Dvd 和流媒體,VUDU,Netflix 流媒體,iTunes,亞馬遜和亞馬遜 Prime,頂級 DVD 和流媒體,新版本,即將上映DVD,認證新鮮電影,瀏覽全部,熱門電影,預告片,論壇,查看全部,查看全部,熱門電視節目,認證新鮮電視,24 幀,所有時間串列,狂歡指南,電視漫畫,倒計時,評論家共識,五部最喜歡的電影,現在流媒體,家長指導,紅地毯綜述,記分卡,亞崇拜,全面召回,視頻采訪,周末票房,每周番茄醬,看什么,零點,查看全部,查看全部,查看全部,它發生了一晚(1934),公民凱恩(1941),綠野仙蹤(1939),現代(1936),黑豹(2018),寄生蟲(Gisaengchung)(2019),復仇者聯盟:殘局(2019),卡薩布蘭卡 (1942), 利刃出鞘 (2019), 我們 (2019), 玩具總動員 4 (2019), 伯德小姐 (2017), 碟中諜 - 輻射 (2018), 黑幫 (2018), 滾出 (2017),愛爾蘭人 (2019)、教父 (1972)、瘋狂的麥克斯:狂暴之路 (2015)、蜘蛛俠:平行宇宙 (2018)、月光 (2016)、日落大道 (1950)、關于夏娃 (1950) , 卡利加里博士的內閣 (Das Cabinet des Dr. Caligari) (1920), 費城故事 (1940), 羅馬 (2018), 神奇女俠 (2017), 一個明星的誕生 (2018), 由內而外 (2015) , 一個安靜的地方 (2018), 邁阿密的一晚 (2020), 八年級 (2018), 麗貝卡 (1940), Booksmart (2019), 洛根 (2017), 他的女孩星期五 (1940), 著火女士的肖像(Portrait de la jeune fille en feu)(2020),可可(2017),敦刻爾克(2017),星球大戰:最后的絕地武士(2017),歌劇院之夜(1935),水的形狀(2017),雷神:諸神黃昏(2017),聚光燈(2015),告別(2019),塞爾瑪(2014),第三個人(1949),后窗(1954),ET外星人( 1982), 七武士 (Shichinin no Samurai) (1956), 大幻覺 (Grand Illusion) (1938), 到達 (2016), 雨中歌唱 (1952), 寵兒 (2018), 雙倍賠償 (1944) , 西線無戰事 (1930), 白雪公主和七個小矮人 (1937), 婚姻故事 (2019), 大病 (2017), 在海濱 (1954), 星球大戰第七集 - 原力覺醒(2015), 一個美國人在巴黎 (1951), 我們生命中最美好的歲月 (1946), 大都會 (1927), 少年時代 (2014), 地心引力 (2013), 不留痕跡 (2018), 馬耳他獵鷹 (1941) 、隱形人(2020)、為奴十二年(2013)、好萊塢往事(2019)、Argo (2012), Soul (2020), Ma Rainey's Black Bottom (2020), The Kid (1921), Manchester by the Sea (2016), Nosferatu, a Symphony of Horror (Nosferatu, eine Symphonie des Grauens) (Nosferatu the Vampire) ) (1922)、羅賓漢歷險記 (1938)、愛樂之城 (2016)、西北偏北 (1959)、勞拉 (1944)、蜘蛛俠:英雄遠征 (2019)、超人總動員 2 (2018)、 Zootopia (2016), Alien (1979), King Kong (1933), Shadow of a Doubt (1943), Call Me by Your Name (2018), Psycho (1960), 1917 (2020), LA Confidential (1997), The佛羅里達計劃 (2017), 猩球崛起 (2017), 帕丁頓熊 2 (2018), 艱難的一天的夜晚 (1964), 寡婦 (2018), 從不罕見有時總是 (2020), 嬰兒司機 (2017),蜘蛛俠:英雄歸來(2017),教父,第二部分(1974),阿爾及爾戰役 (La Battaglia di Algeri) (1967), 查看全部, 查看全部]]
uj5u.com熱心網友回復:
pandas.read_html()通過@F.Hoque 提供的閱讀表格可能會更精簡,但您也可以BeautifulSoup只使用它來獲得結果。
遍歷所有<tr>,從via /<table>中選擇資訊并將其結構化存盤在 dicts 串列中:tags.text.get_text()
data = []
for row in soup.select('table.table tr')[1:]:
data.append({
'rank': row.td.text,
'title': row.a.text.split(' (')[0].strip(),
'releaseYear': row.a.text.split(' (')[1][:-1]
})
例子
import requests
from bs4 import BeautifulSoup
url = "https://www.rottentomatoes.com/top/bestofrt/"
headers = {"Accept-Language": "en-US, en;q=0.5"}
result = requests.get(url=url)
soup = BeautifulSoup(result.text, 'html.parser')
data = []
for row in soup.select('table.table tr')[1:]:
data.append({
'rank': row.td.text,
'title': row.a.text.split(' (')[0].strip(),
'releaseYear': row.a.text.split(' (')[1][:-1]
})
data
輸出
[{'rank': '1.', 'title': 'It Happened One Night', 'releaseYear': '1934'},
{'rank': '2.', 'title': 'Citizen Kane', 'releaseYear': '1941'},
{'rank': '3.', 'title': 'The Wizard of Oz', 'releaseYear': '1939'},
{'rank': '4.', 'title': 'Modern Times', 'releaseYear': '1936'},
{'rank': '5.', 'title': 'Black Panther', 'releaseYear': '2018'},...]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/442003.html
