【问题标题】:Parsing NBA reference with python beautiful soup用python美汤解析NBA参考
【发布时间】:2017-01-29 10:26:48
【问题描述】:

所以我正在尝试使用 python 和美丽的汤从这个网站http://www.basketball-reference.com/leagues/NBA_2016.html 刮出杂项统计数据表。这是到目前为止的基本代码,我只想看看它是否正在读取表格,但是当我打印表格时,我什么也没有。

from bs4 import BeautifulSoup
import requests
import pandas as pd 

url = "http://www.basketball-reference.com/leagues/NBA_2016.html"
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data)

table = soup.find('table', id='misc_stats')
print table

当我检查网页本身的 html 时,我想要的表格出现在前面带有这个符号的 <!-- 并且该部分的 html 文本是绿色的。我能做些什么?

【问题讨论】:

    标签: python html parsing beautifulsoup


    【解决方案1】:

    <!-- 是注释的开头,--> 是 html 的结尾,所以在解析之前删除 cmets:

    from bs4 import BeautifulSoup
    import requests
    comm = re.compile("<!--|-->")
    
    html = requests.get("http://www.basketball-reference.com/leagues/NBA_2016.html").content
    cleaned_soup = BeautifulSoup(re.sub("<!--|-->","", html))
    
    
    tableStats = cleaned_soup.find('table', {'id':'team_stats'})
    
    print(tableStats)
    

    【讨论】:

    • 非常感谢。效果很好。
    猜你喜欢
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2020-11-23
    • 2014-08-11
    • 2013-04-29
    相关资源
    最近更新 更多