BeautifulSoup打印空元素



我正试图通过从跟踪网站抓取和存储pvp统计数据,学习使用beautifulsoup和selenium的网络抓取(和python(。我正在抓取的网站有一个元素,可以显示你赢得比赛的机会,我正在努力获取这个价值。问题是:它打印为一个空白元素("[]"(。我尝试过使用.text,但这引发了一个异常。我相信这是一个简单的解决办法,我只是有点力不从心。我的完整代码在下面,似乎在做我需要它做的事情。

from selenium import webdriver
from selenium.webdriver.common.by import By
import codecs
import time
from bs4 import BeautifulSoup as soup
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.maximize_window()
driver.get("https://destinytracker.com/destiny-2/profile/bungie/4611686018453544077/overview")
html = driver.page_source
page_soup = soup(html, "html.parser")
element = driver.find_elements(By.CLASS_NAME, "match-row--expandable")
for i in element:
i.click()
time.sleep(2)
close = driver.find_element_by_class_name("close")
chance = page_soup.find_all('div', {'class' : 'match-chance'})#pull match chance
close.click()
print(chance)
driver.close()

您可能不需要仅对一行使用bs4。你还不如用硒本身。你可以用chance = driver.find_element(By.CLASS_NAME, 'match-chance')代替chance = page_soup.find_all('div', {'class' : 'match-chance'})#pull match chance,你会得到一些结果。

您的代码重构:

from selenium import webdriver
from selenium.webdriver.common.by import By
import codecs
import time
#from bs4 import BeautifulSoup as soup
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.maximize_window()
driver.get("https://destinytracker.com/destiny-2/profile/bungie/4611686018453544077/overview")
# html = driver.page_source
# page_soup = soup(html, "html.parser")
element = driver.find_elements(By.CLASS_NAME, "match-row--expandable")
for i in element:
i.click()
time.sleep(3)
# chance = page_soup.find('div', {'class' : 'match-chance'})#pull match chance
chance = driver.find_element(By.CLASS_NAME, 'match-chance')
print(chance.text)
close = driver.find_element(By.CLASS_NAME, "close")
close.click()
driver.close()

输出:

Bravo had a 42% chance of winning!
Alpha had a 92% chance of winning!
Bravo had a 81% chance of winning!
Alpha had a 64% chance of winning!
Alpha had a 52% chance of winning!
Bravo had a 75% chance of winning!

最新更新