从div中抓取数据



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

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

我想知道球员的名字、姓氏和得分。我已经这样写过了,但是它没有打印任何东西。

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

请检查您这边的代码

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

希望对你有帮助。

最新更新