Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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 beautifulsoup未返回预期结果?_Python_Beautifulsoup - Fatal编程技术网

Python beautifulsoup未返回预期结果?

Python beautifulsoup未返回预期结果?,python,beautifulsoup,Python,Beautifulsoup,我正在读一个巨大的文件。进行简单搜索会返回明显不正确的结果 代码如下: import urllib2 import re from bs4 import BeautifulSoup data = urllib2.urlopen('http://www.nhl.com/scores/htmlreports/20132014/PL020749.HTM') html = data.read() soup = BeautifulSoup(html, 'lxml') a = soup.findAll

我正在读一个巨大的文件。进行简单搜索会返回明显不正确的结果

代码如下:

import urllib2
import re
from bs4 import BeautifulSoup

data = urllib2.urlopen('http://www.nhl.com/scores/htmlreports/20132014/PL020749.HTM')
html = data.read()

soup = BeautifulSoup(html, 'lxml')

a = soup.findAll('td')

print len(a) 
这就产生了

2737

但是,只要手动搜索
,我也有同样的问题。以这种方式尝试其他解析器:

soup = BeautifulSoup(html, 'html5lib')

这将解决您的问题。

我按原样运行了您的代码,得到了您提到的
td
元素的确切数目,15562。我怀疑您需要将您的
lxml
版本或Beauty soup版本更新为最新版本。。我有lxml3.2.4和bs3.2。1@qwwqwwq我刚刚运行了
sudo apt get--purge remove python-bs4
pip uninstall beautifulsoup
pip install lmxl--upgrade
但仍然得到2737。我对此感到非常恼火。还有其他地方可以安装有故障的beautifulsoup吗?请注意,我使用python的默认html解析器运行了此程序,并且得到了2737,因此我假设beautifulsoup存在问题,那么您使用的python版本是什么?@qwwqwq2.7。我已经删除了BS4的每一种方式,我知道如何仍然可以导入它