我正在使用BeautifulSoup4抓取页面,并且以下功能给了我2个问题:
def getTeamRoster(teamURL):
html = urllib.request.urlopen(teamURL).read()
soup = BeautifulSoup(html)
teamPlayers = []
#second table
corebody = soup.find(id = "corebody")
teamTable = corebody.table.next_sibling.next_sibling.next_sibling.next_sibling
print(teamTable)
tableBody = teamTable.find('tbody')
print(tableBody)
tableRows = tableBody.findAll('tr')
1)当我仅调用4次“ .next_sibling”(如上所述)时,我似乎得到了正确的表。但是,我尝试访问的表标签是#corebody ID中的第6个表。当我调用“ .next_sibling” 5次时,我从BeautifulSoup得到-1,表明我请求的表不存在?我以为您通常会在这种情况下使None退回。知道为什么调用“ .next_sibling” 5次不能按预期工作吗?
网址为 http://modules.ussquash.com/ssm/pages/leagues/Team_Information.asp?id=11325
2)tableBody = teamTable.find('tbody')给我带来了一些麻烦。当我打印tableBody时,我没有任何提示,但是我不确定为什么会发生这种情况(我正在访问的表中肯定有一个标签)。
有想法吗?
感谢您的帮助,bclayman
我可以使用pandas.read_html
以下方法获取玩家表:
import requests
import pandas as pd
url = 'http://modules.ussquash.com/ssm/pages/leagues/Team_Information.asp?id=11325'
tables = pd.read_html(requests.get(url).content)
tables[4]
\n\t\t\t\tPlayers\n\t\t\t City Gender SinglesRating TeamPosition Expiration Win/Loss P Registered Code Ref. Exam
0 Browne,Noah Taunton M 5.56 1 02/29/2016 14 / 4 - 08/28/14 - NaN
1 Ellis,Thornton rye M 4.27 10 02/29/2016 0 / 9 - 08/28/14 - pass
2 Line,James Glastonbury M 4.25 10 02/29/2016 2 / 7 - 08/28/14 - NaN
3 Desantis,Scott J. Sudbury M 5.08 2 02/29/2016 9 / 10 - 08/28/14 - pass
4 Bahadori,Cameron Great Falls M 4.97 3 01/12/2016 3 / 10 - 11/05/14 - pass
5 Groot,Michael Victoria M 4.76 4 02/29/2016 5 / 11 - 08/28/14 - NaN
6 Ehsani,Darian Greenwich M 4.76 5 02/29/2016 6 / 13 - 08/28/14 - pass
7 Kardon,Max Weston M 4.83 6 02/29/2016 5 / 14 - 08/28/14 - pass
8 Van,Jeremy NaN M 4.66 7 02/29/2016 5 / 13 - 08/28/14 - NaN
9 Southmayd,Alexander T. Boston M 4.91 8 02/29/2016 13 / 6 - 08/28/14 - pass
10 Cacouris,Stephen A Alpine M 4.68 9 02/29/2016 9 / 10 - 08/28/14 - pass
11 Groot,Christopher Edmonton M 4.62 - 02/29/2016 0 / 2 - 08/28/14 - NaN
12 Mack,Peter D. (sub) N. Eastham M 3.94 - 02/29/2016 0 / 1 - 11/23/14 - NaN
13 Shrager,Nathaniel O. Stanford M 0.00 - 02/29/2016 0 / 0 - 08/28/14 - NaN
14 Woolverton,Peter C. Chestnut Hill M 4.06 - 02/29/2016 1 / 0 - 08/28/14 - NaN
15 Total Players: 15 Average singles rating: 4.36... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句