如何修复attributeError:resultset对象没有属性' get_text'
我试图根据星星从GitHub获取3D主题的顶级存储库。
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( ))
这是错误
属性:ResultSet对象没有属性“文本”。您可能正在处理像单个项目一样的项目列表。当您打算致电find()时,您是否致电find_all()?
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()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
beautifuresoup find_all()方法返回结果集对象。您可以通过使用Type()函数来验证这一点:
要从每个元素获取文本,请尝试循环浏览每个元素,然后在其上调用get_text()方法。
BeautifulSoup find_all() method returns resultset object. You can verify that by using type() function:
To get the text from each element, try to loop through each element and then call get_text() method on it.
您可能需要使用循环的
循环循环:
You might want to loop through the results, using a
for
loop: