我知道这可能是一个简单的问题,但我确实需要这里的帮助。
我正在尝试从此汤对象中提取每场比赛的总篮板数。
我尝试使用以下代码,但出现错误:
import urllib.request
from bs4 import BeautifulSoup
import csv
url = "https://www.basketball-reference.com/players/a/abdulza01.html"
request = urllib.request.Request(url) # create request object
response = urllib.request.urlopen(request)
html = response.read().decode('unicode_escape') # convert to unicode format
soup = BeautifulSoup(html, "html.parser")
table = soup.find('table', attrs={'id': 'per_game'})
results = table.find_all('tr')
for result in results[1:len(results)]:
data = result.find_all('td')
data.find(attrs={'data-stat': 'trb_per_g'}).getText()
data = [<td class="center iz" data-stat="age"></td>,
<td class="left " data-stat="team_id"><a href="/teams/BOS/">BOS</a></td>,
<td class="left " data-stat="lg_id">NBA</td>,
<td class="center iz" data-stat="pos"></td>,
<td class="right " data-stat="g">2</td>,
<td class="right incomplete iz" data-stat="gs"></td>,
<td class="right " data-stat="mp_per_g">12.0</td>,
<td class="right " data-stat="fg_per_g">1.5</td>,
<td class="right " data-stat="fga_per_g">6.5</td>,
<td class="right " data-stat="fg_pct">.231</td>,
<td class="right " data-stat="ft_per_g">1.0</td>,
<td class="right " data-stat="fta_per_g">1.5</td>,
<td class="right " data-stat="ft_pct">.667</td>,
<td class="right " data-stat="orb_per_g">3.0</td>,
<td class="right " data-stat="drb_per_g">4.5</td>,
<td class="right " data-stat="trb_per_g">**7.5**</td>,
<td class="right " data-stat="ast_per_g">1.5</td>,
<td class="right " data-stat="stl_per_g">0.5</td>,
<td class="right " data-stat="blk_per_g">0.5</td>,
<td class="right " data-stat="tov_per_g">1.5</td>,
<td class="right " data-stat="pf_per_g">2.0</td>,
<td class="right " data-stat="pts_per_g">4.0</td>]
错误消息: AttributeError:ResultSet对象没有属性“ find”。您可能正在将项目列表像单个项目一样对待。当您打算致电find()时,您是否致电过find_all()?
代码在概念上有问题吗?
答案 0 :(得分:1)
我认为这是您的问题的答案:Beautiful Soup: 'ResultSet' object has no attribute 'find_all'?
ResultSet对象没有属性'find'。您可以做的是访问每个元素,然后使用“查找”找到所需的内容。
答案 1 :(得分:0)
使用x['users'].idxmin()
值搜索时,还应该提供attribute
名称。
请尝试以下代码。如果仅要搜索一个元素,请尝试tag
。如果要搜索多个元素,请尝试find
,然后迭代循环。希望这会有所帮助。
find_all
要搜索多个项目,请尝试此操作。
from bs4 import BeautifulSoup
html="""<html><td class="center iz" data-stat="age"></td>,
<td class="left " data-stat="team_id"><a href="/teams/BOS/">BOS</a></td>,
<td class="left " data-stat="lg_id">NBA</td>,
<td class="center iz" data-stat="pos"></td>,
<td class="right " data-stat="g">2</td>,
<td class="right incomplete iz" data-stat="gs"></td>,
<td class="right " data-stat="mp_per_g">12.0</td>,
<td class="right " data-stat="fg_per_g">1.5</td>,
<td class="right " data-stat="fga_per_g">6.5</td>,
<td class="right " data-stat="fg_pct">.231</td>,
<td class="right " data-stat="ft_per_g">1.0</td>,
<td class="right " data-stat="fta_per_g">1.5</td>,
<td class="right " data-stat="ft_pct">.667</td>,
<td class="right " data-stat="orb_per_g">3.0</td>,
<td class="right " data-stat="drb_per_g">4.5</td>,
<td class="right " data-stat="trb_per_g">**7.5**</td>,
<td class="right " data-stat="ast_per_g">1.5</td>,
<td class="right " data-stat="stl_per_g">0.5</td>,
<td class="right " data-stat="blk_per_g">0.5</td>,
<td class="right " data-stat="tov_per_g">1.5</td>,
<td class="right " data-stat="pf_per_g">2.0</td>,
<td class="right " data-stat="pts_per_g">4.0</td></html>"""
soup = BeautifulSoup(html,'html.parser')
findtag=soup.find('td',attrs={"data-stat" : "trb_per_g" })
print(findtag.text)
答案 2 :(得分:0)
我认为使用css选择器组合按目标td单元的表ID和attribute = value定位会更快
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
url = "https://www.basketball-reference.com/players/a/abdulza01.html"
soup = bs(requests.get(url).content, 'lxml')
data = [item.text for item in soup.select('#per_game [data-stat=trb_per_g]')]
df = pd.DataFrame(data)
df.rename(columns=df.iloc[0], inplace = True)
df.drop(df.index[0], inplace = True)
print(df)
df.to_csv(r'C:\Users\Users\Desktop\Data.csv', sep=',', encoding='utf-8',index = False )