所以我试着用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文本是绿色的。我能做什么?
发布于 2016-09-21 17:07:59
<!--是注释的开头,而-->是注释的结尾,所以在解析之前只需删除注释即可:
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)https://stackoverflow.com/questions/39612000
复制相似问题