Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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 Can';当我通过类过滤器和字典搜索标签时,在beautifulsoup中找不到标签_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python Can';当我通过类过滤器和字典搜索标签时,在beautifulsoup中找不到标签

Python Can';当我通过类过滤器和字典搜索标签时,在beautifulsoup中找不到标签,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,下面是我的代码` import urllib from BeautifulSoup import * html=urllib.urlopen('http://yellowpages.sulekha.com/coffee-shops-bars-restaurants_delhi') soup=BeautifulSoup(html) tags=soup.findAll("li",{ "class" : "list-item" }) print tags 我希望找到所有具有类“列表项”的“li”标

下面是我的代码`

import urllib
from BeautifulSoup import *
html=urllib.urlopen('http://yellowpages.sulekha.com/coffee-shops-bars-restaurants_delhi')
soup=BeautifulSoup(html)
tags=soup.findAll("li",{ "class" : "list-item" })

print tags
我希望找到所有具有类“列表项”的“li”标记。当我执行上述代码时,它返回一个空列表,但当我将其键入

tags=soup.findall("li","list-item")

它返回包含所有必需的“li”标记的正确列表。谁能告诉我为什么会这样?我正在使用Python2.7和beautifulsoup3。

我去查看了您试图搜索的站点,并搜索了
列表项。然后我看到了一些东西,看起来像下面的

<li class="list-item " itemtype="http://schema.org/LocalBusiness" itemscope="">

  • 我看到类是
    “列表项”
    ,而不是
    “列表项”
    。当我在代码中添加额外的空间时,它会返回包含所有列表项的列表。

    无法对python 2.7发表评论,但是上面的代码——您所说的代码不起作用,在python 3.5上运行良好。我刚刚将
    urllib.urlopen()
    更改为
    html=urllib.request.urlopen()
    ,并修复了urllib.urlopen()
    中的虚假空间。我甚至不好意思对您的回复表示感谢。在徒劳地寻找解决办法之后,我感到厌烦,但从来没有考虑过这个小错误。非常感谢你。