Home > Mobile >  How to fix AttributeError: ResultSet object has no attribute 'get_text'
How to fix AttributeError: ResultSet object has no attribute 'get_text'

Time:05-31

I'm trying to get the top repositories of 3d topic from github based on the stars.

topic_page_url = 'http://github.com/topics/3d'
response = requests.get(topic_page_url)
topic_doc = BeautifulSoup(response.text,'html.parser')

star_tags = topic_doc.find_all('span',{'class':'Counter js-social-count'})
#print(star_tags[0].text) has given result 11.k
def parse_star_count (stars_str):
    stars_str = stars_str.strip()
    if stars_str[-1] == 'k':
      return int(float(stars_str[:-1]) * 1000)  
    return int(stars_str)

#it's working if it's only one element
parse_star_count(star_tags[0].get_text().strip( ))

#if i try to print all it is showing the error 
parse_star_count(star_tags.get_text().strip( )) 

This is the error

AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

CodePudding user response:

You might want to loop through the results, using a for loop:

topic_page_url = 'http://github.com/topics/3d'
response = requests.get(topic_page_url)
topic_doc = BeautifulSoup(response.text,'html.parser')

star_tags = topic_doc.find_all('span',{'class':'Counter js-social-count'})
#print(star_tags[0].text) has given result 11.k
def parse_star_count (stars_str):
    stars_str = stars_str.strip()
    if stars_str[-1] == 'k':
      return int(float(stars_str[:-1]) * 1000)  
    return int(stars_str)

for star_tag in star_tags:
    parse_star_count(star_tag.get_text().strip( ))

CodePudding user response:

BeautifulSoup find_all() method returns resultset object. You can verify that by using type() function:

type(star_tags)
bs4.element.ResultSet

To get the text from each element, try to loop through each element and then call get_text() method on it.

for star in star_tags:
    print(star.get_text())
  • Related