【发布时间】:2020-11-13 00:12:24
【问题描述】:
我正在尝试通过此链接收集有关 2020 年全球最高薪运动员收入的信息 https://www.forbes.com/profile/roger-federer/?list=athletes 这是第一页的代码
import requests
from bs4 import BeautifulSoup
import csv
page = requests.get('https://www.forbes.com/profile/roger-federer/?list=athletes')
soup = BeautifulSoup(page.text, 'html.parser')
profile = soup.find(class_ = 'profile-content')
name = soup.find(class_='profile-heading__rank').next_sibling
value = profile.find(class_ = "profile-info__item-value").get_text()
stats = profile.find_all(class_ = "profile-stats__text")
age = stats[0].get_text()
sport = stats[1].get_text()
citizenship = stats[5].get_text()
photo = profile.find(class_ = "profile-photo")
image = photo.find("img")
source=image.get("src")
print(name+" " +" "+ value+" "+ source+" "+age+" "+sport+" "+citizenship)
如何通过点击next的分页获取剩余99名运动员的详细信息
【问题讨论】:
-
从按钮中提取超链接并下载它,“点击”按钮在beautifulsoup中不起作用。
-
使用 class "profile-nav__next" 的 href 加载 a 元素,这将为您提供“下一个”要获取的 URL。
-
@NathanChampion 代码示例
-
我不懂 Python。但这似乎很简单,使用 profile.find(class_ = "profile-nav__next").get("href")。将其存储在一个变量中,然后是 requests.get(NextPageVariable)。您可能需要预先添加 URI,并且需要某种结束条件。
标签: python html web-scraping beautifulsoup pagination