Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Beautifulsoup'错误;结果集';对象没有属性';findAll';_Python_Beautifulsoup_Runtime Error - Fatal编程技术网

Python Beautifulsoup'错误;结果集';对象没有属性';findAll';

Python Beautifulsoup'错误;结果集';对象没有属性';findAll';,python,beautifulsoup,runtime-error,Python,Beautifulsoup,Runtime Error,我在处理代码中的一些错误,如下所示: #!/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)

我在处理代码中的一些错误,如下所示:

#!/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)
所以,这里有代码,它连接到谷歌,搜索表单(soupstrainer),一切正常,但问题是这向我显示了这个错误:

txtinput = forms1.findAll('input', {'type': 'text'})
AttributeError: 'ResultSet' object has no attribute 'findAll'
我认为错误在于forms1数据是一个列表,但我不明白如何修改代码使其工作


谢谢大家

是,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)
此问题的副本: