问题
Essentially, I am attempting to extract the text from the table with the given class title below. I have the rest of the code already written that extracts the text from each of the rows, so I do not need any assistance with that aspect. I just cannot seem to figure out why I am receiving this error:
"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()?
The code is:
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'})
回答1:
The problem is that you wrote a find_all to find the region. As a result, it generates a set of results, not just a single one (of course the set can contain one, zero or more results). There are two options I think:
If you are sure that there is only one div with that id (normally there should only be one, you can use a
find:region = soup.find('div', {'id': 'inning-all'}) table = region.find('table', {'class': 'sidearm-table play-by-play'})In case there are multiple ones: iterate over the founded regions, and process them separately:
If you are sure that there is only one div with that id (normally there should only be one, you can use a
find:regions = soup.find_all('div', {'id': 'inning-all'}) for region in regions: table = region.find('table', {'class': 'sidearm-table play-by-play'})
回答2:
As an alternative, you may approach the problem with a single CSS selector:
table = soup.select_one('#inning-all table.sidearm-table.play-by-play')
where the CSS selector would match a table element with sidearm-table and play-by-play classes under the element with inning-all id attribute value.
Use select() instead of select_one() to locate all elements matching a selector.
来源:https://stackoverflow.com/questions/44620432/beautifulsoup-cannot-locate-table-with-specific-class