添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
绅士的勺子  ·  python ...·  昨天    · 
千年单身的咖啡  ·  python pip ...·  昨天    · 
大鼻子的消防车  ·  spyder下报错ModuleNotFoun ...·  15 小时前    · 
叛逆的洋葱  ·  python 新环境的创建并在 ...·  15 小时前    · 
路过的大象  ·  学习 | ...·  1 年前    · 
奔放的松树  ·  how to redirect the ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.