soup.find_all和find_elements_by_class返回未找到任何元素

问题描述

我尝试了两种方法来找到没有结果的下注奇数。我什么也没得到。

这是代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import pandas as pd

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.optibet.lv/sport/wcg/CS:GO-5541")

odds = driver.find_elements_by_class('event-block-row__odd event-block-row__odd_clickable event-block-row__odd_without-middle-odd')

if odds is not None:
    print('found odds element')
    print(odds)

这不起作用。它只是打印“发现的赔率元素”。然后,我尝试将类名更改为odds = driver.find_elements_by_class('odd__value'),但无济于事。 之后,我尝试使用BeautifulSoup:

from selenium import webdriver
from bs4 import BeautifulSoup

url = "https://www.optibet.lv/sport/wcg/CS:GO-5541"
driver = webdriver.Chrome()
driver.get(url)
soup = BeautifulSoup(driver.page_source,'html.parser')

containers = soup.find_all("div",class_="event-block-row__odd event-block-row__odd_clickable event-block-row__odd_without-middle-odd")
print (len(containers))

这将返回“ 0”。我没有主意,也不是很有经验。有帮助吗?

解决方法

在上课之前切换到iframe。然后循环列表。

driver.get("https://www.optibet.lv/sport/wcg/CS:GO-5541")
driver.implicitly_wait(10)
driver.switch_to.frame(driver.find_element_by_css_selector("#iFrameResizer0"))
odds = driver.find_elements_by_class_name('odd__value')

if odds is not None:
    print('found odds element')
    for odd in odds:
        print(odd.text)
,

许多站点都有刮板保护,其次,您的站点非常沉重。您可以尝试一下,但是BeautifulSoup有局限性:

from bs4 import BeautifulSoup
import urllib.request
import bs4 as bs

url_1 = 'https://www.optibet.lv/sport/wcg/CS:GO-5541'
sauce_1  = urllib.request.urlopen(url_1).read()
soup_1 = bs.BeautifulSoup(sauce_1,'lxml')

for table in soup_1.find('div',class_='event-block-row__odd event-block-row__odd_clickable event-block-row__odd_without-middle-odd'):
    print(table.text)