2013-06-23 55 views
1

我正在处理我的代码中的一些错误,这里是:错误与Beautifulsoup式“结果”对象有没有属性“的findAll”

#!/usr/bin/env python2 
# -*- coding: utf-8 -*- 
from BeautifulSoup import BeautifulSoup as beatsop 
from BeautifulSoup import SoupStrainer as sopstrain 
import urllib2 

def html_parser(html_data): 
    html_proc = beatsop(html_data) 
    onlyforms = sopstrain("form") 
    forms1 = html_proc.findAll(onlyforms) 
    txtinput = forms1.findAll('input', {'type': 'text'}) 
    #We take the forms that aren't text 
    listform = ["radio", "checkbox", "password", "file", "image", "hidden"] 
    otrimput = forms1.findAll('input', {'type': listform}) 
    # we seach for names in text forms 
    for e_name in txtinput: 
     names = e_name['name'] 
    #we seach for value in otrimput 
    for e_value in otrimput: 
     value1 = e_value.get('value') 
     if value1: 
      pass 
     else: 
      print('{} there is no value.'.format(e_value)) 

html_data = urllib2.urlopen("http://www.google.com") 
html_parser(html_data) 

因此,有代码,它连接到谷歌, SEACH的形式(soupstrainer),一切都OK了,但问题是,这说明我这个错误:

txtinput = forms1.findAll('input', {'type': 'text'}) 
AttributeError: 'ResultSet' object has no attribute 'findAll' 

我认为错误是forms1数据是一个列表,但我不知道如何修改代码以使其工作。

谢谢大家!

+0

重复这个问题:http://stackoverflow.com/questions/992183/why-am-i-getting-resultset-has-no-attribute-findall-using-beautifulsoup-in?rq=1 – ojs

回答

2

是的,findAll返回一个ResultSet,它是一种列表。所以你可以选择一个值或者遍历它们。下面的代码显示迭代。

#!/usr/bin/env python2 
# -*- coding: utf-8 -*- 
from BeautifulSoup import BeautifulSoup as beatsop 
from BeautifulSoup import SoupStrainer as sopstrain 
import urllib2 

def html_parser(html_data): 
    html_proc = beatsop(html_data) 
    onlyforms = sopstrain("form") 
    forms1 = html_proc.findAll(onlyforms) 
    for found_form in forms1: 
     txtinput = found_form.findAll('input', {'type': 'text'}) 
     #We take the forms that aren't text 
     listform = ["radio", "checkbox", "password", "file", "image", "hidden"] 
     otrimput = found_form.findAll('input', {'type': listform}) 
     # we seach for names in text forms 
     for e_name in txtinput: 
      names = e_name['name'] 
     #we seach for value in otrimput 
     for e_value in otrimput: 
      value1 = e_value.get('value') 
      if value1: 
       pass 
      else: 
       print('{} there is no value.'.format(e_value)) 

html_data = urllib2.urlopen("http://www.google.com") 
html_parser(html_data) 
+0

非常感谢!你救了我 ! –

相关问题