from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import time
def scrape_laptops(driver):
# Laptop listesini içeren üst elementi bul
laptop_list = driver.find_element(By.XPATH, '//*[@id="1"]')
# Liste içindeki tüm laptopları bul
laptops = laptop_list.find_elements(By.TAG_NAME, 'li')
print(laptops)
laptop_data = []
for laptop in laptops:
# Her laptop için model ve fiyat bilgisini çek
model = laptop.find_elements(By.XPATH, '/html/body/div[1]/div/div/main/div/div/div[2]/div/div[2]/div[3]/div/div[2]/div/div/div/div[2]/div/ul[1]/li[1]/div/a/div[2]/div[2]/h3')
for mod in model:
print(mod)
print(model)
price = laptop.find_element(By.XPATH, '/html/body/div[1]/div/div/main/div/div/div[2]/div/div[2]/div[3]/div/div[2]/div/div/div/div[2]/div/ul[1]/li[1]/div/a/div[2]/div[4]')
laptop_data.append({'model': model, 'price': price})
print(price)
print(laptop)
return laptop_data
# Firefox options
options = Options()
options.headless = True
# Start Firefox webdriver
driver = webdriver.Firefox(options=options)
# Go to the Hepsiburada Lenovo computers page
driver.get('https://www.hepsiburada.com/lenovo/bilgisayarlar-c-3000500')
# Accept cookies
button2 = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))
)
button2.click()
# Wait for the page to load
time.sleep(5)
# Scroll to the bottom of the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
# Click the "Show more" button
button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div/div/main/div[1]/div/div[2]/div/div[2]/div[3]/div/div[2]/div/div/div/div/div/div/div[1]/button'))
)
actions = ActionChains(driver)
actions.move_to_element(button).perform()
button.click()
time.sleep(5)
# Scrape laptop models and prices
laptop_data = scrape_laptops(driver)
# Write laptop data to a file
with open("laptop_prices.txt", "w", encoding="utf-8") as file:
for laptop in laptop_data:
file.write(f"Model: {laptop['model']}, Fiyat: {laptop['price']}\n")
# Close the webdriver
driver.quit()
我可以在页面上获取笔记本电脑的列表。当我打印笔记本电脑列表时,我得到这个“
你有
x = driver.find_element(...)
print(x)
问题在于
x
包含 WebElement
,而不是该元素中包含的文本。
您需要添加
.text
才能获取该元素中包含的文本。更新上面的示例后,我们得到
x = driver.find_element(...).text
print(x)