Home > database >  python selenium - extract numeric string, convert to int and find median
python selenium - extract numeric string, convert to int and find median

Time:08-24

I'm working on extracting the value (string) of an object from the site, converting it to an int, and finding the intermediate value.

driver.get('https://www.coupang.com/np/search?q=시서스&channel=user&component=&eventCategory=SRP&trcid=&traid=&sorter=scoreDesc&minPrice=&maxPrice=&priceRange=&filterType=&listSize=72&filter=&isPriceRange=false&brand=&offerCondition=&rating=0&page=9&rocketAll=false&searchIndexingToken=1=6&backgroundColor=')

prara = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'price-value'))) 

y = []
for j in prara:
    print(j)
    o = j.text
    s = o.replace(",", "")
    y  = s

prval = statistics.median(y)
print(prval)
    
>>>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="c39b2f69-4bc9-4ebe-87c4-afa3f1dcaeee")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="f5c2372b-6592-418f-8d31-fe266332cc6d")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="ad22afa5-f29d-444d-a5a0-6c8efe558e31")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="89196683-0c7d-4f0e-8c6c-30218b5e2f07")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="f4153c2a-3a23-4baa-9f13-4bc3e21e1073")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="9b7c1d8a-4420-44d8-977b-b6bf00a4b8d3")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="8d8ab946-832e-4865-9421-3ace5facbb99")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="d5403964-b12f-4506-ab51-6c1f2d2ea2e8")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="89ebdb12-7436-4407-a930-54f124d42d06")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="c314426c-d94e-461d-b526-597532812a1b")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="fcb717fe-a6f8-4aa1-9640-4651fd0efdf6")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="9880ed2a-d09c-48fc-9ffd-aac9792324a2")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="e4252dee-ae4d-42a8-b289-42dac1fc6af3")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="cda7c9a2-5957-4861-aead-19f9d643b53b")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="ae9d1f9c-65e0-44a0-9df2-73e2a4171ea0")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="3fda6be7-7338-4f65-858e-a9a266e6219e")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="a84f64cd-8397-424b-9f9c-f8987cfbc26b")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="c0097638-060e-479c-b8a9-8694c498459b")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="e375ffc4-ef6f-4c8f-aa09-398fa1f2efa5")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="481bf4e6-54f9-4999-8727-1f75a9dac033")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="1d65f8d3-0448-4d67-aff3-2f55ee83071b")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="7c5bc5f3-be42-49ce-83ad-a3acfd3e5437")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="5f2e1114-4907-4a0e-b9f6-ba748211f4a7")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="927b6a96-2d55-4f20-9a0c-602aede912b3")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="f1028ec1-419c-4c20-829b-ee976bad5e6a")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="4b57a9f4-b0e3-487d-bb56-9901ef9901c8")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="37fa14b1-1014-4b61-82f8-10113630028d")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="287b4979-bc59-42c2-a549-be328a2116fd")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="5d3eb54a-fbe7-4d59-b71b-ee43c2ec3500")>
<selenium.webdriver.remote.webelement.WebElement (session="a0ddfb9d3848df719d38dac2458be774", element="206ae32d-2b68-4915-8839-32a978e3e040")>
2

I tried to extract the value, but I got the wrong number 2. Is there a way to extract a number and find the median?

CodePudding user response:

The following code works:

prara = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'price-value'))) 

y = []
for j in prara:
    print(j.text)
    o = j.text
    s = o.replace(",", "")
    y.append(float(s))

prval = statistics.median(y)
print(prval)

It's returning the median as 109000.0 Let me know if something is unclear.

  • Related