rah*_*f23 1 python beautifulsoup
从本质上讲,我试图从具有下面给定类标题的表格中提取文本。我已经编写了从每一行中提取文本的其余代码,因此我不需要这方面的任何帮助。我似乎无法弄清楚为什么我会收到此错误:
"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
Run Code Online (Sandbox Code Playgroud)
代码是:
from bs4 import BeautifulSoup
import requests
header = {'User-agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'}
url = requests.get("http://www.jsugamecocksports.com/boxscore.aspx?path=baseball&id=4109", headers = header).text
soup = BeautifulSoup(url, 'html.parser')
region = soup.find_all('div', {'id': 'inning-all'})
table = region.find('table', {'class': 'sidearm-table play-by-play'})
Run Code Online (Sandbox Code Playgroud)
问题是你写了一个find_all来查找区域。结果,它生成了一组结果,而不仅仅是单个结果(当然该集合可以包含一个、零个或多个结果)。我认为有两种选择:
如果您确定只有一个具有该 ID 的 div(通常应该只有一个,您可以使用一个find:
region = soup.find('div', {'id': 'inning-all'})
table = region.find('table', {'class': 'sidearm-table play-by-play'})Run Code Online (Sandbox Code Playgroud)
如果有多个:迭代已建立的区域,并分别处理它们:
如果您确定只有一个具有该 ID 的 div(通常应该只有一个,您可以使用一个find:
regions = soup.find_all('div', {'id': 'inning-all'})
for region in regions:
table = region.find('table', {'class': 'sidearm-table play-by-play'})Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
7776 次 |
| 最近记录: |