python - 网络抓取问题(空列表)
问题描述
我目前正在尝试获取玩家的排名,但总是返回一个空列表。挣扎了一段时间,非常感谢一些帮助和未来项目解决这些问题的任何提示。或者总体上更好地理解beautifulsoup的地方
import requests as req
from bs4 import BeautifulSoup
id = "epic"
tag = "random"
url = f"https://rocketleague.tracker.network/rocket-league/profile/{id}/{tag.lower()}/overview"
html = req.get(url).content
soup = BeautifulSoup(html,"lxml")
line = soup.findAll("div",{"class":"rank"})
print(line)
这是我想要得到的:
解决方案
在这里加载响应requests
不起作用,仅仅是因为站点动态加载内容,因此您需要使用一些webdriver
.
这是我想出的:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(executable_path=r"driver/geckodriver.exe", options=options)
id = "epic"
tag = "random"
url = f"https://rocketleague.tracker.network/rocket-league/profile/{id}/{tag.lower()}/overview"
driver.get(url)
driver.implicitly_wait(2) #allow some time to fully load, you may tweak accordingly
ranks = driver.find_elements_by_css_selector(r'[class="rank"]')
for i in ranks:
print(i.text)
driver.quit()
导致:
#695,273 • Top 32%
#1,409,786 • Bottom 26%
#1,240,839 • Bottom 43%
#1,195,373 • Bottom 45%
#875,794 • Top 41%
#874,338 • Top 41%
#1,530,195 • Bottom 29%
#960,411 • Top 45%
Unranked Division I
#1,663,447 • Top 33%
Gold III Division III
#2,974,386 • Bottom 29%
Platinum II Division III
#2,879,016 • Bottom 40%
Platinum II Division II
#2,363,741 • Top 50%
Gold III Division III
#2,407,466 • Bottom 42%
Platinum II Division I
#2,054,002 • Top 48%
Gold II Division II
#2,321,030 • Bottom 41%
推荐阅读
- c++ - 为什么自定义转换函数模板不能有推导返回类型?
- swiftui - 出现错误:创建字符串文字时“表达式类型不明确,没有更多上下文” - SwiftUI
- java - 当从列表中选择项目时,该项目不会显示在 textview(EditorFragment)
- angular - 对组件中的存储使用功能状态或根状态有什么区别?
- github - 如何设置 github 操作以运行 headful puppeteer 以进行 E2E 扩展测试
- php - 如何在php中获得相同项目的最低单位价格
- react-native - 如何在(滚动)屏幕中显示 30 页文本
- codeigniter - 要加密的产品 ID
- html - 我的 css 代码中的颜色与我的本地主机服务器上的颜色不匹配
- git - git - 从 git 历史记录中删除关键数据