Bottom Page

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>,...'
Quote
#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
Quote
#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']
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Scrap a dynamic span hefaz 0 776 Mar-07-2020, 02:56 PM
Last Post: hefaz
  Using Python to get attribute text furiousfrodo 2 556 Dec-18-2019, 04:18 PM
Last Post: furiousfrodo
  Cannot get contents from ul.li.span.string LLLLLL 8 640 Nov-29-2019, 10:30 AM
Last Post: LLLLLL
  How to get visible text of Select Drop down ankitjindalbti 2 643 Jun-03-2019, 12:35 AM
Last Post: ankitjindalbti
  Error object has no attribute text hcyeap 3 7,304 May-21-2019, 07:12 AM
Last Post: buran
  BeautifulSoup 'NoneType' object has no attribute 'text' bmccollum 9 7,315 Sep-14-2018, 12:56 PM
Last Post: bmccollum
  selenium click a span tag metulburr 1 14,911 Nov-30-2016, 05:47 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)