从div抓取数据

2024-06-06 20:08:40 发布

您现在位置:Python中文网/ 问答频道 /正文

早上好,当我想从div抓取数据时,我没有什么问题。例如,我在网站上有一个结构,如:

<div class="score-result">
Player1Name
Player1Surname
<div>score</div>
</div>

我想知道球员的名字、姓氏和分数。我写过这样的smth,但它不打印任何东西

def trade_spider(max_hall,max_period):
    hall=2
    period=1
    while hall <= max_hall:
        url ='https://tabletennis.setkacup.com/en/schedule?date=2021-08-27&hall=' +str(hall)+'&'+'period='+str(period)
        source_code = requests.get(url)
        plain_text=source_code.text
        soup=BeautifulSoup(plain_text, "html.parser")
        for link in soup.findAll('table', {'class': 'score-result'}):
            score = link.get('score-result')
            print(score)
            hall=+1
            period=+1

Tags: textdivurlsourcegetcoderesultmax
1条回答
网友
1楼 · 发布于 2024-06-06 20:08:40

请检查您这边的代码

import requests
import os
import time
from bs4 import BeautifulSoup
from selenium import webdriver


service = webdriver.chrome.service.Service(os.path.abspath('chromedriver'))
service.start()
option = webdriver.ChromeOptions()


driver = webdriver.Chrome(os.path.abspath('chromedriver'), options=option)

hall = 2
period = 1
while hall <= 5:
    url = 'https://tabletennis.setkacup.com/en/schedule?date=2021-08-27&hall=' + \
        str(hall)+'&'+'period='+str(period)
    driver.get(url)
    time.sleep(5)
    divs = driver.find_elements_by_css_selector("div.score-result")
    for div in divs:
        # you can add this code
        try :
            fund = div.find_element_by_tag_name("div").text
            print(fund)
        catch :
            pass

        print(div.text)

    hall = hall + 1

希望能对您有所帮助

相关问题 更多 >