如果你看一下网页本身,它的表格下面有一系列建议页面的链接(你已经知道了):
对我来说,鼠标悬停不会显示目标URL,但是如果你打开浏览器devtools(不是R的devtools
包),通常是F12
之类的,转到"网络"选项卡,然后点击其中一个页面,你会看到获取的URL:
您可以使用该模板来迭代表,直到不再获得任何数据.
read_html("https://www.finanznachrichten.de/w/289/1?tab=0&page=26") |>
html_table()
# [[1]]
# # A tibble: 20 × 5
# Zeit `Aktuelle NachrichtenSprache: \nAlle DE EN` Medien `` ``
# <chr> <chr> <chr> <lgl> <chr>
# 1 14:15 "Danish non-alc producer ISH talks up \"tremendous\" potential in off-premise" Just Drinks NA ""
# 2 14:15 "Nicht erschrecken, wenn diese Google-Seite plötzlich einen neuen Look hat" SmartDroid NA ""
# 3 14:15 "Neue Gemini-App für Android startet, aber nicht in Deutschland" SmartDroid NA ""
# 4 14:15 "Small launch companies seek niches to compete with SpaceX rideshare" SpaceNews NA ""
# 5 14:15 "Netflix enthüllt alle Highlights für 2024" TECHBOOK NA ""
# 6 14:15 "Höhere Prämien treiben Hannover Rück an" Versicherungsmagazin NA ""
# 7 14:15 "China's NEV exports up 27.1 pct in January 2024: association" Xinhuanet NA ""
# 8 14:15 "Bayer: Neue Probleme am Horizont" 4investors NA "In den USA bek…
# 9 14:15 "Gratis war gestern: Nur noch 4 Prozent der Onlinehändler bieten kostenlosen Versand" t3n NA "Immer weniger …
# 10 14:15 "Tata Communications arbeitet mit Microsoft zusammen, um Sprachanrufe über Microsoft Teams zu verbessern" news aktuell NA "Mumbai, Indien…
# 11 14:15 "IAA Transportation 2024 mit Bus-Themenpark" Automobil Industrie NA ""
# 12 14:15 "Mofas, Mopeds, E-Scooter: Schilderwechsel steht bevor" cash-online.de NA ""
# 13 14:15 "Wachtendorf-Kolumne: Zertifikate-Schwemme: Tatort Sparkasse" DAS INVESTMENT NA ""
# 14 14:15 "Volvo Global All-Electric Car Sales Improved In January Thanks To EX30" InsideEVs NA ""
# 15 14:15 "Playtech nets 2028 extension to Boylesports partnership" SBC News NA ""
# 16 14:15 "Mysteries of the Orient: Unveil the Secrets with Endorphina's Oriental Dragon" European Gaming NA ""
# 17 14:15 "Politiker gehen viral: Wieso kann wirklich jeder ein TikTok-Star werden?" futurezone NA ""
# 18 14:15 "Aufgewärmter Reis ist \"tödlich\": Das steckt hinter der TikTok-Panik" futurezone NA ""
# 19 14:15 "Spritzer inks JV agreement to develop housing project in Perak" The Edge Markets NA ""
# 20 14:15 "Court strikes out LCS subcontractors' lawsuit against BHIC, directors" The Edge Markets NA ""
不幸的是……page=27
返回相同的表格,所以我花了一些时间才知道page=26
是最后一页……我没有信心它会一直是=26
.特别是因为你想活到polite
岁,而我不能提前知道有多少页,你会想要非常小心地寻找已经被退回的any rows页.下面是一个稍微可以接受的过程来获得所有这些信息:
tables <- list()
page <- 1
while (TRUE) {
this <- read_html(paste0("https://www.finanznachrichten.de/w/289/1?tab=0&page=", page)) |>
html_table()
if (any(sapply(tables, function(tb) anyDuplicated(rbind(tb, this[[1]])) > 0))) break
tables <- c(tables, this)
page <- page + 10 # use +1 in reality, I'm just being `polite` :-)
}
page
tables <- do.call(rbind, tables)
tables
# # A tibble: 80 × 5
# Zeit `Aktuelle NachrichtenSprache: \nAlle DE EN` Medien `` ``
# <chr> <chr> <chr> <lgl> <chr>
# 1 14:51 "Opening Bell: PayPal, Adyen, Roblox, PepsiCo, GoPro, Mattel" Der Aktion… NA "Wie…
# 2 14:51 "PayPal: Die Kursziele purzeln" finanztref… NA "Pay…
# 3 14:51 "Siemens: Allzeithoch!" finanztref… NA "Die…
# 4 14:51 "Von Studis bis zu Beauty & Health: APG mit 18 neuen Zielgruppen im Digitalen" Klein Repo… NA ""
# 5 14:51 "Krebsforschung: Deutschland kann beim Forschungsboom nicht mithalten" RND.de NA ""
# 6 14:51 "Betreiber zuversichtlich: Gasspeicher erneut zu 100 Prozent befüllbar" RND.de NA ""
# 7 14:50 "ERSTE AG: Suspension of trading in and the removal of EBBMWTS14 turbo warrant and the possibility of Residual Value Trading" Budapest S… NA ""
# 8 14:50 "Universal Investment: \"Stehen nicht im Wettbewerb mit Kunden\"" fondscheck… NA "Wie…
# 9 14:50 "Rosenberger, Langer & Cie. ernennt Andreas Langer zum Geschäftsführer" fondscheck… NA "Bad…
# 10 14:50 "Stabilitas Gold+Resourcen Special Situations-Fonds: 01/2024-Bericht, Wertrückgang von 11,3%" fondscheck… NA "Bad…
# # ℹ 70 more rows
# # ℹ Use `print(n = ...)` to see more rows
备注:
- 我在这里加
page + 10
是为了演示,你可能想要page + 1
.
- 出于许多原因,我 Select 不对
while
循环中的每一遍都进行tables <- rbind(tables, this[[1]])
操作(特别是,请参见The R Inferno,"Growing Objects");在这种情况下,它可能"很好",但它可能很快就会失控,所以这种将tables
内的每个表rbind
与this[[1]]
相加的方法限制了内存的增长.