beautifulsoup webscraper问题:无法在网页上找到表格

时间:2018-12-18 12:15:15

标签: python web-scraping beautifulsoup findall

我想使用以下代码从this网站获取表格:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.flashscore.pl/pilka-nozna/'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
containers = page_soup.find_all('table', {'class': 'soccer'})

print(len(containers))

但是当我尝试检查print(len(containers))得到多少张表时,我得到0。 有解决方案吗?

编辑: image of contained tables

1 个答案:

答案 0 :(得分:4)

页面可能是动态的。您可以使用requests-html,它允许您在拉出html之前先渲染页面,也可以使用Selenium,就像我在这里所做的那样。

这产生了表class =“ soccer”的42个元素

import bs4 
from selenium import webdriver 

url = 'https://www.flashscore.pl/pilka-nozna/'

browser = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')
browser.get(url)

html = browser.page_source
soup = bs4.BeautifulSoup(html,'html.parser')  

containers = soup.find_all('table', {'class': 'soccer'})

browser.close()


In  [11]: print(len(containers))
42