I'm working on something to scrape all element texts, somehow sort the number and get the title in it. I've gotten to the point where I can print all element texts but I can't seem to get the lowest number and title. I'm also only trying to get the elements that are "CARdano4SPEED__", and the number should only be 1-1000, if it isn't in that range, it shouldn't print. It should also only just get the lowest one of that. I'm not really sure how to go about that so any advice would be helpful, thanks!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.jpg.store/collection/cardano4speed?filters=%7B%22engine%22%3A%5B%22v8%20special%20edition%22%5D%7D")
elements = driver.find_elements(By.XPATH, '//*[@id="asset-card"]')
for element in elements:
print(element.text)
Which outputs:
CARdano4SPEED1120
1000
For Sale
CARdano4SPEED118
1111
For Sale
CARdano4SPEED117
5000
For Sale
CodePudding user response:
You should use dot .
to create relative xpath and get title, price in every element
separatelly.
Later you can get title
, remove "CARdano4SPEED"
and convert rest into int()
. And now you can check if it in range 1-1000
and display or not.
And if you put it on list with price
(converted to int()
) as first then you can use min()
to get element with lower price.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.jpg.store/collection/cardano4speed?filters=%7B%22engine%22%3A%5B%22v8%20special%20edition%22%5D%7D")
data = []
elements = driver.find_elements(By.XPATH, '//*[@id="asset-card"]')
for element in elements:
title = element.find_element(By.XPATH, './/h4').text
price = element.find_element(By.XPATH, './/span[@id="asset-price"]').text
prince = int(price)
number = int(title.replace("CARdano4SPEED", ""))
if number <= 1000:
print('title:', title)
print('price:', price)
print('number:', number)
print('---')
data.append( [price, number, title] )
# ---
price, number, title = min(data)
print('min:', price, number, title)
Result:
title: CARdano4SPEED118
price: 1111
number: 118
---
title: CARdano4SPEED117
price: 5000
number: 117
---
min: 1111 118 CARdano4SPEED118