2017-05-08 132 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 –