2017-05-08 146 views
0

我試圖從'http://www.flashscore.com/'提取足球係數表。當您查看頁面的源代碼時,您可以看到該表格位於id =「fs」的div內。但是,當我搜索該div時,BeautifulSoup不返回任何內容。我寫了如下腳本。這裏有什麼問題?美麗的湯4 HTML解析

Code 
import requests 
from bs4 import BeautifulSoup 

r = requests.get("http://www.flashscore.com/") 
soup = BeautifulSoup(r.content, "lxml") 
print(soup.find(id="fs")) 
+0

'table'來自'post'請求,你不能從'get'請求中提取它。 –

+0

所以你說這是不可能從這個網站提取這些係數? –

+0

我什麼時候說過這是不可能的? –

回答

2

您,是因爲數據(帶班FS格)裝有ajax.When request.get('http://www.flashscore.com/')只使用'http://www.flashscore.com/'網址是requested.No其他Ajax請求被稱爲是與它相關聯使用selenium。 參考低於使用硒代碼

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

driver = webdriver.Firefox() 
driver.get("http://www.flashscore.com/") 
try: 
    element = WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((By.ID, "fs")) 
    ) 
finally: 
    driver.quit() 
+0

錯誤:selenium.common.exceptions.WebDriverException:消息:'geckodriver'可執行文件需要位於PATH –

+0

@ElginCahangirov您必須安裝geckodriver請檢查此http://selenium-python.readthedocs.io/installation.html#drivers –

+0

謝謝你的幫助!您確定無法使用請求模塊執行此操作嗎? –

0

我找不到與 'FS' 任何潛水ID上flashscore.com

import requests 
from bs4 import BeautifulSoup 
r = requests.get("http://www.flashscore.com/") 
soup = BeautifulSoup(r.text, "html.parser") 
print(soup.find('div',id='fsbody')) 

soup.find()給出,如果ID 的第一次出現你想找到所有你可以利用find_all()函數

+0

我試過這個。湯發現ID爲'fsbody'的div,但無法找到id爲'fs'的div。那就是問題所在。看看ID爲'fsbody'的div的內部,'fs'在那裏。 –

+0

我找不到任何帶有id ** fs **的div –