AttributeError:"NoneType"对象没有属性"findAll" [英] AttributeError: 'NoneType' object has no attribute 'findAll'
问题描述
我必须解析此网页 http://india.gov.in/topics/health-family-welfare/health
我应该在这里找到我的代码的标题,这根本不打印标题有什么问题
I am supposed to get the headings my code is here ,it is not at all printing the headings what's wrong
#!/usr/bin/env python
import urllib2
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
import sys
import csv
mech = Browser()
url = "http://www.india.gov.in/topics/health-family-welfare/health"
page = mech.open(url)
html = page.read()
soup = BeautifulSoup(html)
div=soup.find("div",{"class":"view-content"})
lists = div.find('ol')
for ol in lists:
lis=ol.findAll('li')
print lis
print
她是我遇到的错误
File "srap_req.py", line 17, in <module>
lists = div.findAll('ol')
AttributeError: 'NoneType' object has no attribute 'findAll'
推荐答案
div
为None
,因为在前一行的.find()
调用未找到匹配的<div class="view-content">
.您加载的页面上没有这样的div.如果要解析多个页面,则必须考虑到.find()
调用实际上不会找到您要查找的对象,而是返回None
.
div
is None
, because on the preceding line the .find()
call did not find a matching <div class="view-content">
. You loaded a page without such a div on it. If you are parsing multiple pages, you have to take into account that .find()
calls won't actually find the object you are looking for and return None
instead.
我强烈建议您改为在此处切换到BeautifulSoup 4. BeautifulSoup 3两年多来还未发现任何新版本的错误修复程序,BeautifulSoup 4允许您使用 CSS查询以获取所需内容,这很容易:
I strongly recommend you switch to BeautifulSoup 4 here instead. BeautifulSoup 3 hasn't seen any new releases of bug fixes for over 2 years now, and BeautifulSoup 4 lets you use CSS queries to get what you want, which is a lot easier:
from bs4 import BeautifulSoup
mech = Browser()
url = "http://www.india.gov.in/topics/health-family-welfare/health"
page = mech.open(url)
soup = BeautifulSoup(page)
for item in soup.select('div.view-content ol li'):
print item
如果您使用的是mechanize
,则可能还需要查看 RoboBrowser ,它是一个更现代的版本-实现,使用requests
和BeautifulSoup:
If you are using mechanize
, you may also want to look at RoboBrowser, a more modern re-implementation, using requests
and BeautifulSoup:
from robobrowser import RoboBrowser
browser = RoboBrowser()
url = "http://www.india.gov.in/topics/health-family-welfare/health"
browser.open(url)
for item in browser.select('div.view-content ol li'):
print item
这篇关于AttributeError:"NoneType"对象没有属性"findAll"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!