from bs4 import BeautifulSoup
from urllib import urlopen
player_code = open("/Users/brandondennis/Desktop/money/CF_Name.txt").read()
player_code = player_code.split("\r")
for player in player_code:
html =urlopen("https://www.capfriendly.com/players/"+player+"")
soup = BeautifulSoup(html, 'html.parser')
for section in soup.findAll('div',{"class": "table_c"}):
table = section.findChildren()[10].text
print player, table
这里是示例播放器页面的链接:https://www.capfriendly.com/players/patrik-elias
以下是我从文本文件添加到基本网址的玩家名称示例。
这就是我最终想要为包含 1000 多名玩家的文本文件所做的事情
最佳答案
除了其他人提到的。看一下这一行:
table = soup.findAll('table_c')[2]
这里,BeautifulSoup
将尝试定位 table_c
元素。但是,table_c
是一个类属性:
<div class="table_c"><div class="rel navc column_head3 cntrct"><div class="ofh"><div>HISTORICAL SALARY </div><div class="l cont_t mt4">SOURCE: The Hockey News, USA Today</div></div></div>
<table class="cntrct" id="contractinsert" cellpadding="0" border="0" cellspacing="0">
...
</table>
</div>
改用class_
参数:
table = soup.find_all(class_='table_c')[2]
或者,您可以通过id
直接进入表格:
table = soup.find("table", id="contractinsert")
关于python - 使用 Python 从网页中抓取表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37884990/