Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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_Urllib2_Html5lib - Fatal编程技术网

Python Beautifulsoup功能在特定场景中无法正常工作

Python Beautifulsoup功能在特定场景中无法正常工作,python,beautifulsoup,urllib2,html5lib,Python,Beautifulsoup,Urllib2,Html5lib,我试图使用urllib2:读入以下url,然后在数据中搜索元重定向 它在中读取以下数据: <!--?xml version="1.0" encoding="UTF-8"?--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:/

我试图使用urllib2:读入以下url,然后在数据中搜索元重定向

它在中读取以下数据:

   <!--?xml version="1.0" encoding="UTF-8"?--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><meta content="0;url= Home.html" http-equiv="refresh"/></head><body></body></html>
没有结果

我的最终目标是:

    soup.find("meta",attrs={"http-equiv":"refresh"})
但如果:

    soup.findAll('meta')

甚至都不工作,那我就卡住了。任何对这个秘密的煽动都将不胜感激,谢谢

是注释和doctype将解析器抛出到这里,然后是美化组

甚至HTML标记也似乎“消失了”:

>>> soup.find('html') is None
True
但它仍然存在于
目录中。您可以通过以下方式再次找到内容:

for elem in soup:
    if getattr(elem, 'name', None) == u'html':
        soup = elem
        break

soup.find_all('meta')
演示:

>对于汤中的元素:
...     如果getattr(elem,'name',None)==u'html':
...         汤=元素
...         打破
... 
>>>汤。查找所有('meta')
[]

您使用的是什么版本的Beautifulsoup?使用
导入请求;来自bs4进口美联;BeautifulSoup(requests.get(your_url)).find_all('meta')
对我来说很好。嗨,乔恩,谢谢你的快速回复。我正在使用bs4。然而,为了导入,我使用httplib2和html5lib解析数据。根据您的回答和Martijn的回答,这似乎是错误的根源。看起来您正在使用请求库(来自python-requests.org)使其工作。我不知道该资源,我将进一步使用它,谢谢!感谢您的大力鼓励和评论,谜团终于解开了!这几天我一直在为这个问题绞尽脑汁,谢谢你明确而迅速的回复。
for elem in soup:
    if getattr(elem, 'name', None) == u'html':
        soup = elem
        break

soup.find_all('meta')
>>> for elem in soup:
...     if getattr(elem, 'name', None) == u'html':
...         soup = elem
...         break
... 
>>> soup.find_all('meta')
[<meta content="0;url= Home.html" http-equiv="refresh"/>]