Красивый суп извлечение тела - PullRequest
1 голос
/ 03 февраля 2020

Я пытаюсь извлечь данные из tbody с веб-страницы. Я могу получить доступ к части tbody, но пришел пустой запрос. Как я могу решить это? Спасибо. Мой ожидаемый результат, как есть tr и td, попал в этот tbody, но ничего не получилось только так: <tbody class="matchCentreStatsContainer"></tbody>

import requests
from bs4 import BeautifulSoup

Abs_Conc = []
url = 'https://www.premierleague.com/match/46605'
res = requests.get(url)
html_page = res.content
soup = BeautifulSoup(html_page, 'html.parser')

try:
    Tbody = soup.find('div', {'class': 'matchCentre'}).find('section', {'class': 'mcContent'}).find('div', {'class': 'centralContent'}).find('div', {'class': 'mcTabsContainer'}).find('section', {'class': 'mcMainTab head-to-head'}).find('div', {'class': 'mcStatsTab statsSection season-so-far wrapper col-12'}).select('tbody', {'class': 'matchCentreStatsContainer'})

1 Ответ

0 голосов
/ 03 февраля 2020

Попробуйте этот сценарий Selenium, он дал мне список со статистикой:

    # Import libraries

    from selenium import webdriver 
    from selenium.webdriver.support.ui import WebDriverWait  
    from selenium.webdriver.support import expected_conditions as EC  
    from selenium.common.exceptions import TimeoutException 
    import numpy as np
    import pandas as pd

    # Add incognito argument to webdriver
    liv_option = webdriver.ChromeOptions()
    liv_option.add_argument(' — incognito')

    # Request URL
    liv_browser = webdriver.Chrome(executable_path= '/Users/Chrome/chromedriver.exe', 
                          chrome_options=liv_option) #check your correct path here!!
    liv_browser.get('https://www.premierleague.com/match/46605')

    #first click the tab 'stats'
    liv_timeout = 20 #time out, to load the page properly
    try:
       WebDriverWait(liv_browser,
              liv_timeout).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mainContent"]/div/section/div[2]/div[2]/div[1]/div/div/ul/li[3]'))).click()

       print("Successfully opened in the browser!")
    except TimeoutException:
       print('Page took too long to load or there was a different problem :(')
       _browser.quit()  

    #now get the needed element
    try:    
      liv = liv_browser.find_elements_by_xpath('//*[@id="mainContent"]/div/section/div[2]/div[2]/div[2]/section[3]/div[2]/div[2]/table/tbody')
      liv_stats = [x.text for x in liv]

    except:
      print('this element can not be found')

    #get the output (a list)
    liv_stats
...