【问题标题】:Beautifulsoup how to get all div id values from the css selectorBeautifulsoup 如何从 css 选择器中获取所有 div id 值
【发布时间】:2020-01-22 16:36:48
【问题描述】:

我正在尝试从该站点获取所有 ID 匹配项。首先,我下载表格以及我将如何为我刚刚下载的所有内容提供打印标签,但是当我尝试获取值 div.id 时,我有 Non; ( 我的意思是这里包含的 ID:

    <div class = "event__match event__match - last event__match - oneLine" id = "g_1_ARFva552" title = "Click for match detail!">

如果有人可以帮助我,我该如何下载所有匹配 id ...

这是我的代码:

browser.get("https://www.flashscore.com/football/")
sleep(3)
source = browser.page_source # Get the entire page source from the browser
if browser is not None :browser.close() # No need for the browser so close it 
soup = BeautifulSoup(source,'html.parser')
try:
    Tags = soup.select('div.leagues--live') # get the elements using css selectors
    print(Tags)
    for tag in Tags: # loop through them 
        matchId = tag.find('div').get('id')
        print (matchId)


except Exception as e:
    print(e)

提前感谢您的帮助

【问题讨论】:

标签: html python-3.x selenium beautifulsoup


【解决方案1】:

如果您使用 selenium 和 bs4,则诱导 WebDriverWait 并等待 visibility_of_element_located() 而不是 sleep()

使用以下css选择器返回所有具有id属性的div元素。

代码:

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

browser=webdriver.Chrome()
browser.get("https://www.flashscore.com/football/")
WebDriverWait(browser,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"div.leagues--live")))
source = browser.page_source # Get the entire page source from the browser
if browser is not None :browser.close() # No need for the browser so close it
soup = BeautifulSoup(source,'html.parser')
try:
    Tags = soup.select("div.leagues--live div[title='Click for match detail!'][id]") # get the elements using css selectors
    for tag in Tags: # loop through them
        print (tag['id'])
except Exception as e:
    print(e)

输出

g_1_tlPhaQm9
g_1_Cx3yi2ek
g_1_G6H5dOXR
g_1_dh16mtAI
g_1_8WUO5NPn
g_1_tlkj9gx4
g_1_fH8eMl74
g_1_l4weOAxh
g_1_2sC3KSyH
g_1_MVOy2KLk
g_1_K4aSodm5
g_1_MDNDnZxN
g_1_ptl2EDRi
g_1_v3aeymC2
g_1_t6GdSgqn
g_1_bsB1RDbh
g_1_xY95QXDb
g_1_Wf99PiT4
...so on

【讨论】:

    猜你喜欢
    • 2015-01-04
    • 2017-01-27
    • 2016-10-18
    • 1970-01-01
    • 2016-02-08
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    相关资源
    最近更新 更多