python - Python Beautiful Soup 返回无类型
问题描述
我正在尝试开发一个程序,可以为英雄联盟中的特定冠军获取符文。这是我的代码:
import requests
import re
from bs4 import BeautifulSoup
url = 'https://www.leagueofgraphs.com/zh/champions/builds/darius'
response = requests.get(url).text
soup = BeautifulSoup(response,'lxml')
tables = soup.find('div',class_ = 'img-align-block')
print(tables)
这是原始的 HTML 文件:
<img src="//cdn2.leagueofgraphs.com/img/perks/10.8/64/8010.png" alt="征服者" tooltip="<itemname><img src="//cdn2.leagueofgraphs.com/img/perks/10.8/64/8010.png" width="24" height="24" alt="征服者&quot; /> 征服者</itemname><br/><br/>基礎攻擊或技能在命中敵方英雄時獲得 2 層征服者效果,持續 6 秒,每層效果提供 2-5 適性之力。 最多可以疊加 10 次。遠程英雄每次普攻只會提供 1 層效果。<br><br>在疊滿層數後,你對英雄造成的 15% 傷害會轉化為對自身的回復效果(遠程英雄則為 8%)。" height="36" width="36" class="requireTooltip">
我无法访问这部分并对其进行解析,也无法找到 IMG src。但是,我可以在他们的网站上浏览这个。我该如何解决这个问题?
解决方案
您感兴趣的部分不在 HTML 中。您可以通过搜索仔细检查:
soup.prettify()
网站的某些部分可能加载了 JavaScript,因此您可以使用打开浏览器并访问该页面的代码。例如,您可以使用硒
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get(url)
time.sleep(6) # give the website some time to load
page = driver.page_source
soup = BeautifulSoup(page,'lxml')
tables = soup.find('div', class_='img-align-block')
print(tables)
推荐阅读
- jenkins - Jenkins & Newman:HTMLEXTRA 报告使用 Jenkins html 发布者在项目级别发布,仅用于成功的 Jenkins 构建
- java - 带有java 11的Tomcat未知类加载器
- google-cloud-platform - 在 Google Cloud Endpoints 中为外部客户端授权
- javascript - 在 ng-circle-progress 结束时如何做完整的循环?
- cuba-platform - CUBA 平台是否可以防止标签页被多次打开?
- java - 在 Android Studio 中保存实例状态:Kotlin
- wireshark - 将 pcap 文件转换为 csv:对于某些数据包,Tshark 在一行中显示多个 src,dst IP 地址
- c# - 使用 ProtectedData.Unprotect() 函数的 C# 错误
- elasticsearch - 使用外部托管的 Elasticsearch 实例进行高级搜索
- docker - 如何使用 dockerfile 安装任何应该在主机中可用的软件?