Python:AttributeError:"ResultSet"对象没有属性"get" [英] Python: AttributeError: 'ResultSet' object has no attribute 'get'

查看:309
本文介绍了Python:AttributeError:"ResultSet"对象没有属性"get"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从网站上抓取一个值并将其放入有效负载请求中时,出现错误消息:

When I try to scrape a value from a website and put it into a payload request I get the error:

AttributeError: 'ResultSet' object has no attribute 'get'

这是我的代码:

resumeURL='url'
response=self.session.get(resumeURL,headers=headers)
soup=BeautifulSoup(response.content, "html.parser")

product=soup.find_all('input',{'name':'_CsrfToken', 'type':'hidden'})
payload = {
    '_CsrfToken':product.get('value')

当我将find_all更改为find时,出现错误:

When I change find_all to find I get the error:

AttributeError: 'NoneType' object has no attribute 'get'

我在做什么错了?

推荐答案

直接从美丽的汤中提取文档:

Taken straight from the beautiful soup documentation:

AttributeError:'ResultSet'对象没有属性'foo'-通常会发生这种情况,因为您希望find_all()返回单个标记或字符串.但是find_all()返回标签和字符串的列表 –一个ResultSet对象.您需要遍历该列表,并查看每个列表的.foo.或者,如果您真的只想要一个结果,则需要使用find()而不是find_all().

AttributeError: 'ResultSet' object has no attribute 'foo' - This usually happens because you expected find_all() to return a single tag or string. But find_all() returns a list of tags and strings–a ResultSet object. You need to iterate over the list and look at the .foo of each one. Or, if you really only want one result, you need to use find() instead of find_all().

因此,如果要获得所有结果(而不仅仅是一个结果),则需要遍历所有ResultSet(例如product),并寻找每个结果集的.get. 像这样:

So if you want all the results -and not just the one- you need to iterate over all your ResultSet (e.g. product) and look for the .get of each one. So something like:

for val in product:
  #check the val.get('value') for each member of list
  print val.get('value')

这篇关于Python:AttributeError:"ResultSet"对象没有属性"get"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆