我使用soup.findAll('table')来尝试在html文件中找到该表,但它不会出现 . 该表确实存在于文件中,并且使用正则表达式,我可以通过这种方式找到它:
import sys
import urllib2
from bs4 import BeautifulSoup
import re
webpage = open(r'd:\samplefile.html', 'r').read()
soup = BeautifulSoup(webpage)
print re.findall("TABLE",webpage) #works, prints ['TABLE','TABLE']
print soup.findAll("TABLE") # prints an empty list []
我知道我正在 生产环境 汤,因为我这样做:
print [tag.name for tag in soup.findAll(align=None)]
它将正确打印它找到的标签 . 我已经尝试过不同的方法来编写“TABLE”,如“table”,“Table”等 . 另外,如果我打开文件并使用文本编辑器编辑它,它上面有“TABLE” .
为什么beautifulsoup找不到 table ?
1 回答
上下文
python 2.x
BeautifulSoup HTML解析器
问题
findall
不返回所有预期的标记,或者根本不返回任何标记,即使用户知道标记存在于标记中解决方案
BeautifulSoup
构造函数时指定确切的解析器基本原理
另见