BeautifulSoup无法找到具有特定类的表 [英] BeautifulSoup cannot locate table with specific class

查看:105
本文介绍了BeautifulSoup无法找到具有特定类的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试从具有以下给定类标题的表中提取文本.我已经编写了剩下的代码来从每一行中提取文本,因此我在这方面不需要任何帮助.我只是似乎无法弄清楚为什么会收到此错误:

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()?

代码是:

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'})

推荐答案

问题是您写了 find_all来查找区域.结果,它会生成结果集,而不仅仅是一个结果集(当然,该结果集可以包含一个,零个或多个结果).我认为有两种选择:

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:

  1. 如果确定具有该ID的只有一个div(通常应该只有一个div,则可以使用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:

如果确定具有该ID的只有一个div(通常应该只有一个div,则可以使用find:

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'})

这篇关于BeautifulSoup无法找到具有特定类的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆