Python Forum
select all the span text with same attribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
select all the span text with same attribute
#1
after finding all spans in the object I get the results as following.
I would like to choose the different values using:
rating_chart[31].find('span',attrs={'class':"_3fVK8yi6"}).getText()

but it only shows the first answer: 1961. How do I get all the numbers: 1961, 437,116,40?

Thank you so much.

results:
'<span class="_3fVK8yi6">1,961</span>,
<span class="_2DzayJ9y"><span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:16.9445521519969%"></span></span></span>,
<span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:16.9445521519969%"></span></span>,
<span class="_3EekUCk7 _2-7CMSau" style="width:16.9445521519969%"></span>,
<span class="_3fVK8yi6">437</span>,
<span class="_2DzayJ9y"><span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:4.497867390461419%"></span></span></span>,
<span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:4.497867390461419%"></span></span>,
<span class="_3EekUCk7 _2-7CMSau" style="width:4.497867390461419%"></span>,
<span class="_3fVK8yi6">116</span>,
<span class="_2DzayJ9y"><span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:1.5509887553315238%"></span></span></span>,
<span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:1.5509887553315238%"></span></span>,
<span class="_3EekUCk7 _2-7CMSau" style="width:1.5509887553315238%"></span>,
<span class="_3fVK8yi6">40</span>,...'
Reply
#2
JennyYang

Hi there! What do you think in use the page content and re.search or re.findall function?

Almost like this:

import re
import urllib.request
var_page_content = urllib.request.urlopen("https://urlthatyouneed.com/").read()
result = re.search(var_page_content, '1961') # it will return 1961 in string format
print(result)
Otherwise, use:

import re
import urllib.request
var_page_content = urllib.request.urlopen("https://urlthatyouneed.com/").read()
result = re.findall(r'[0-9]+', var_page_content) # it will return all the numbers in string format
print(result)
Hope it can help you!

Regards,
Martinelli
Reply
#3
Martinelli regex is not the best choice when it comes to HTML/XML.
There is a reason why parses exits,if want a funny read .
from bs4 import BeautifulSoup

html = '''\
<span class="_3fVK8yi6">1,961</span>,
<span class="_2DzayJ9y"><span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:16.9445521519969%"></span></span></span>,
<span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:16.9445521519969%"></span></span>,
<span class="_3EekUCk7 _2-7CMSau" style="width:16.9445521519969%"></span>,
<span class="_3fVK8yi6">437</span>,
<span class="_2DzayJ9y"><span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:4.497867390461419%"></span></span></span>,
<span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:4.497867390461419%"></span></span>,
<span class="_3EekUCk7 _2-7CMSau" style="width:4.497867390461419%"></span>,
<span class="_3fVK8yi6">116</span>,
<span class="_2DzayJ9y"><span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:1.5509887553315238%"></span></span></span>,
<span class="RSnM6YUj"><span class="_3EekUCk7 _2-7CMSau" style="width:1.5509887553315238%"></span></span>,
<span class="_3EekUCk7 _2-7CMSau" style="width:1.5509887553315238%"></span>,
<span class="_3fVK8yi6">40</span>'''

soup = BeautifulSoup(html , 'lxml')
Usage test.
>>> all_3fV = soup.find_all(class_="_3fVK8yi6")
>>> all_3fV
[<span class="_3fVK8yi6">1,961</span>,
 <span class="_3fVK8yi6">437</span>,
 <span class="_3fVK8yi6">116</span>,
 <span class="_3fVK8yi6">40</span>]
>>> 
>>> [tag.text for tag in all_3fV]
['1,961', '437', '116', '40']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 3.9 : BeautifulSoup: 'NoneType' object has no attribute 'text' fudgemasterultra 1 8,812 Mar-03-2021, 09:40 AM
Last Post: Larz60+
  trying to scrape a span inside a div using beautifulsoup CompleteNewb 2 8,794 Jan-28-2021, 01:20 PM
Last Post: snippsat
  Scrap a dynamic span hefaz 0 2,658 Mar-07-2020, 02:56 PM
Last Post: hefaz
  Using Python to get attribute text furiousfrodo 2 2,819 Dec-18-2019, 04:18 PM
Last Post: furiousfrodo
  Cannot get contents from ul.li.span.string LLLLLL 8 3,949 Nov-29-2019, 10:30 AM
Last Post: LLLLLL
  How to get visible text of Select Drop down ankitjindalbti 2 2,709 Jun-03-2019, 12:35 AM
Last Post: ankitjindalbti
  Error object has no attribute text hcyeap 3 13,837 May-21-2019, 07:12 AM
Last Post: buran
  BeautifulSoup 'NoneType' object has no attribute 'text' bmccollum 9 14,502 Sep-14-2018, 12:56 PM
Last Post: bmccollum
  selenium click a span tag metulburr 1 21,835 Nov-30-2016, 05:47 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020