Reputation: 45
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!
Upvotes: 1
Views: 8897
Reputation: 3098
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)
Upvotes: 2