【问题标题】:Python Beautifulsoup cannot get svg tagsPython Beautifulsoup 无法获取 svg 标签
【发布时间】:2021-08-10 20:42:58
【问题描述】:

我试图让每个小圆圈显示哪个结果是庄家或闲家。我需要从每 35 个表中获取结果。

from selenium import webdriver
import time
from bs4 import BeautifulSoup

driver = webdriver.Chrome('C:/chromedriver/chromedriver.exe')
driver.get('http://ggl-maxim.com/')

driver.find_element_by_xpath('//*[@id="body"]/div/div[2]/div/div[2]/fieldset/input[1]').send_keys('tnrud3080')
driver.find_element_by_xpath('//*[@id="body"]/div/div[2]/div/div[2]/fieldset/input[2]').send_keys('tnrud3080')
driver.find_element_by_xpath('//*[@id="body"]/div/div[2]/div/div[2]/fieldset/button[1]').click()

time.sleep(2)
driver.get('http://ggl-maxim.com/api/popup/popup_menu.asp?mobile=0&lobby=EVOLUTION')

time.sleep(10)
html = driver.page_source
bs = BeautifulSoup(html, 'html.parser')
targets = bs.find_all('svg')

print(targets)

但是,通过此代码,我无法获取 svg 标记。我该怎么办?

否则,如果有人知道如何获得这个赌场的价值,请告诉我。

【问题讨论】:

  • 为什么要用两次driver.get?您正在测试哪个页面?
  • 因为我需要向 Evolution Game 发送我的用户信息。没有它,我无法看到游戏正常运行
  • 你能在截图上找到你要找的圈子吗?有>3000svgs
  • 我编辑了它!我需要将这些记录放入列表中..

标签: python selenium selenium-webdriver beautifulsoup


【解决方案1】:

对于 Selenium,我会使用 css 选择器:

driver.find_elements_by_css_selector(".svg--1nrnH")

Selenium 更新:等待 iframe。这就是找不到元素的原因。

from selenium import webdriver
import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get('http://ggl-maxim.com/')

driver.find_element_by_xpath('//*[@id="body"]/div/div[2]/div/div[2]/fieldset/input[1]').send_keys('tnrud3080')
driver.find_element_by_xpath('//*[@id="body"]/div/div[2]/div/div[2]/fieldset/input[2]').send_keys('tnrud3080')
driver.find_element_by_xpath('//*[@id="body"]/div/div[2]/div/div[2]/fieldset/button[1]').click()

time.sleep(2)
driver.get('http://ggl-maxim.com/api/popup/popup_menu.asp?mobile=0&lobby=EVOLUTION')
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it("gameIframe"))
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".svg--1nrnH")))
targets = driver.find_elements_by_css_selector(".svg--1nrnH")
res = []
for el in targets:
    print(len(el.get_attribute("innerHTML")))
    res.append(el.get_attribute('innerHTML'))
print(*res, sep='\n')

【讨论】:

  • 如果您在顶部看到捕获,我需要将这些结果放入列表中。
  • 谢谢!我将不得不找出如何在列表中使用这些值..
  • 不客气。这是可能的,但需要更多的思考。
  • 好吧,我认为很难用 x,y 位置制作记录列表。我需要更多帮助。你可以吗?
【解决方案2】:

要查找 SVG 标记,请使用以下 xpath:

//[name()='svg']

我看到您没有切换到 iframe,并且您尝试提取的所有元素都在 iframe 中,因此需要切换驱动程序焦点以获取所有所需的元素。

driver.switch_to.frame(driver.find_element_by_id("gameIframe"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2021-09-12
    • 2020-12-06
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 2020-03-11
    相关资源
    最近更新 更多