简体   繁体   中英

Error with Beautifulsoup 'ResultSet' object has no attribute 'findAll'

I was dealing with some error in my code, here is it:

#!/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)

so, there is the code, it connects to google, and seach for the forms(soupstrainer), everything its ok, but the problem is that this shows me this error:

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

I think the error is that forms1 data is a list, but i can't understand how to modify the code to make it work.

Thanks to all!

Yes, findAll returns a ResultSet which is a type of list. So you can either select a value or iterate through them. The code below shows iteration.

#!/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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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