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

纵饮孤独 提交于 2020-04-30 10:10:05

问题


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'

This is my code:

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')

When I change find_all to find I get the error:

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

What am I doing wrong?


回答1:


Taken straight from the beautiful soup documentation:

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().

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')



回答2:


I think the get method should be used in one element of the ResultSet (not on the whole set). I mean, where you do:

product.get('value')

try:

product[0].get('value')


来源:https://stackoverflow.com/questions/49059489/python-attributeerror-resultset-object-has-no-attribute-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!