gpt4 book ai didi

python - 使用 Python 从网页中抓取表格

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:24 24 4
gpt4 key购买 nike

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

以下是我从文本文件添加到基本网址的玩家名称示例。

enter image description here

This is ultimately What I am wanting to do for my text file of 1000+ players

这就是我最终想要为包含 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com