Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python bs4 findAll找不到类标记_Python_Html_Web Scraping_Beautifulsoup - Fatal编程技术网

Python bs4 findAll找不到类标记

Python bs4 findAll找不到类标记,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,我试图通过一个表进行解析,我使用的是bs4。当我使用带有特定类标记的find_all时,不会返回任何内容。但是,当我没有指定类时,它会返回一些内容。i、 这将返回表和所有td元素 from bs4 import BeautifulSoup as soup page_soup = soup(html, 'html.parser') stat_table = page_soup.find_all('table') stat_table = stat_table[0] with open ('s

我试图通过一个表进行解析,我使用的是bs4。当我使用带有特定类标记的find_all时,不会返回任何内容。但是,当我没有指定类时,它会返回一些内容。i、 这将返回表和所有td元素

from bs4 import BeautifulSoup as soup

page_soup = soup(html, 'html.parser')

stat_table = page_soup.find_all('table')
stat_table = stat_table[0]

with open ('stats.txt','w', encoding = 'utf-8') as q:
for row in stat_table.find_all('tr'):
    for cell in row.find_all('td'):
        q.write(cell.text.strip().ljust(18))
如果我尝试使用此选项:

page_soup = soup(html, 'html.parser')

stat_table = page_soup.find_all('table')
stat_table = stat_table[0]

with open ('stats.txt','w', encoding = 'utf-8') as q:
 for row in stat_table.find_all('tr'):
    for cell in row.find_all('td',{'class':'blah'}):
        q.write(cell.text.strip().ljust(18))

此代码应返回具有指定类的特定td元素,但不返回任何内容。任何帮助都将不胜感激。

class属性不是普通字符串,而是.1

例如:

>>> text = "<div><span class='a  b c '>spam</span></div>"
>>> soup = BeautifulSoup(text, 'html.parser')
>>> soup.span['class']
['a', 'b', 'c']
但是,同样,最好使用一个序列或一组单独的值进行搜索,而不是尝试猜测如何将它们组合成一个碰巧可以工作的字符串


所以,你想做的可能是:

for cell in row.find_all('td', {'class': ('column-even', 'table-column-even', 'ft_enrolled')}):

或者,也许您不想用DOM的术语来思考,但是:

>汤。选择('span.a.b.c')
[垃圾邮件]
请注意,CSS也不关心类的顺序或重复项:

>>> soup.select('span.c.a.b.c')
[<span class="a b c">spam</span>]
>>汤。选择('span.c.a.b.c')
[垃圾邮件]
此外,这允许您搜索类的子集,而不仅仅是其中一个或全部:

>>> soup.select('span.c.b')
[<span class="a b c">spam</span>]
>汤。选择('span.c.b')
[垃圾邮件]


一,。这是一个改变,从美丽的汤3。这一点甚至不需要提及,因为BS3已经死了近十年,并且不在Python3.x上运行,在某些情况下甚至不在2.7上运行。但是人们不断地将旧的BS3代码复制粘贴到博客文章和堆栈溢出答案中,所以其他人不断地对他们在网上发现的代码实际上不起作用感到惊讶。如果这里发生了这种情况,您需要学会识别BS3代码,这样您就可以忽略它并查看其他地方。

1)除非我们是会员,否则无法访问该页面。2) 你能不能加入一些html,这样如果链接中断,这个答案仍然有用。如果没有看到html,就没有办法调试它,但是有三个常见的原因导致这个问题:(1)html中甚至不存在
td
;它是在浏览器中运行时由JavaScript代码生成的。(2) 简单的打字错误。(3)
class
是一个多值属性;如果您想将其视为单个字符串,不同的解析器将执行不同的操作,例如将所有运行的空格折叠为单个空格,并且您必须精确匹配解析器执行的任何操作。使用类似于
soup.select('span.column偶数')的操作不是更容易吗
@Barmar CSS选择器是更容易还是更难取决于您是从CSS选择器还是DOM的角度来考虑。但我会把它添加到答案中。
>>> soup.find('span', class_='a  b c ')
>>> soup.find('span', class_='a b c')
<span class="a b c">spam</span>
for cell in row.find_all('td', {'class': ('column-even', 'table-column-even', 'ft_enrolled')}):
>>> soup.select('span.a.b.c')
[<span class="a b c">spam</span>]
>>> soup.select('span.c.a.b.c')
[<span class="a b c">spam</span>]
>>> soup.select('span.c.b')
[<span class="a b c">spam</span>]