繁体   English   中英

Python Beautiful Soup 'NoneType' 对象错误

[英]Python Beautiful Soup 'NoneType' object error

我正在使用 Beautiful Soup 获取网页正文中的超链接。 这是我使用的代码

import urllib2
from bs4 import BeautifulSoup

url = 'http://www.1914-1918.net/swb.htm'
element = 'body'
request = urllib2.Request(url)
page = urllib2.urlopen(request).read()
pageSoup = BeautifulSoup(page)
for elementSoup in pageSoup.find_all(element):
  for linkSoup in elementSoup.find_all('a'):
    print linkSoup['href']

当我尝试查找 swb.htm 页面的超链接时,出现 AttributeError。

AttributeError: 'NoneType' 对象没有属性 'next_element'

我确信在 body 元素下有一个 body 元素和几个“a”元素。 但奇怪的是,它适用于其他页面(例如http://www.1914-1918.net/1div.htm )。

这个问题已经困扰我好几天了。 谁能指出我做错了什么。

截屏

在此处输入图片说明

你的打印错了。 它应该是:

import urllib2
from bs4 import BeautifulSoup

url = 'http://www.1914-1918.net/swb.htm'
element = 'body'
request = urllib2.Request(url)
page = urllib2.urlopen(request).read()
pageSoup = BeautifulSoup(page)
for elementSoup in pageSoup.find_all(element):
  for linkSoup in elementSoup.find_all('a'):
    print linkSoup['href']

对我来说,这会返回很多链接。

如果安装了html5lib,就会发生这种情况。

只需尝试删除它并再次测试。

更多细节: https//bugs.launchpad.net/beautifulsoup/+bug/1184417

异常发生在处理最大递归深度期间。

您收到 AttributeError: 'NoneType' object has no attribute 'something' 因为 NoneType 意味着不是您认为正在使用的任何 Class 或 Object 的实例,而是实际上没有。 这意味着上面的赋值或函数调用失败或返回了意外结果。

也许beautifulsoup4不适合你的Python,尝试删除beautifulsoup4: pip uninstall beautifulsoup4 ,并安装旧版本: pip install beautifulsoup4==<version> ,我使用版本4.1.3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM