from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
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.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver.exe") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://iremedy.com/search?query=Vital Signs Monitors'
browser.get(url)
items_list = []
while True:
elements_on_page = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[class^="card"]')))
print(len(elements_on_page), 'total items found')
if len(elements_on_page) > 100:
print('more than 100 found, stopping')
break
footer = wait.until (EC.presence_of_element_located((By.CSS_SELECTOR, 'footer[id="footer"]')))
footer.location_once_scrolled_into_view
t.sleep(2)
for el in elements_on_page:
title = el.find_element(By.CSS_SELECTOR, 'h3[]')
price = el.find_element(By.CSS_SELECTOR, 'div[]')
items_list.append((title.text.strip(), price.text.strip()))
df = pd.DataFrame(items_list, columns = ['Item', 'Price'])
print(df)
這個網站有一個無限型別的滾動,上面的代碼只是滾動頁面和復制資訊。我想訪問每個產品卡鏈接并復制圖片鏈接、產品名稱、類別、簡短描述、價格、可用性、SKU 和附加資訊。首先,幾個產品鏈接會彈出一個帶有其他類似產品的框,我不知道如何洗掉它。我想復制所需的資訊,然后回傳,然后從下一個繼續。那有可能嗎?
我想讓所有這些程序盡可能快。另一個想法掠過我的腦海,是將無限滾動頁面中存在的所有產品鏈接復制到最后一個產品。之后,執行多個執行緒(如果將其劃分為多個執行緒的好解決方案)從這些鏈接中廢棄。我想知道最快的方法是什么以及如何做。謝謝
uj5u.com熱心網友回復:
這是一種逐個瀏覽每個專案的方法,單擊它,然后訪問每個專案的“更多詳細資訊”彈出視窗。請記住,這只是示范,讓您走上正確的道路:您必須從那里選擇您需要的東西,然后根據需要保存。為了完成這項作業,我必須洗掉頁面中的幾個元素,否則它們會攔截點擊。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
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.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1920,1080")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 30)
url = 'https://iremedy.com/search?query=Vital Signs Monitors'
browser.get(url)
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label="Accept All"]'))).click()
print('accepted cookies')
except Exception as e:
print('no cookie button!')
element_to_be_deleted = browser.find_element(By.CSS_SELECTOR, 'div[id="evidenceClientContainer"]')
second_element_to_be_deleted = browser.find_element(By.CSS_SELECTOR, 'div[data-id="zsalesiq"]')
browser.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", element_to_be_deleted)
browser.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", second_element_to_be_deleted)
items_list = []
while True:
elements_on_page = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[class^="card"]')))
print(len(elements_on_page), 'total items found')
if len(elements_on_page) > 100:
print('more than 100 found, stopping')
break
footer = wait.until (EC.presence_of_element_located((By.CSS_SELECTOR, 'footer[id="footer"]')))
footer.location_once_scrolled_into_view
t.sleep(2)
for el in elements_on_page:
title = el.find_element(By.CSS_SELECTOR, 'h3[]')
price = el.find_element(By.CSS_SELECTOR, 'div[]')
el.find_element(By.CSS_SELECTOR, 'button[]').click()
try:
product_extra_info = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'ul[]'))).text.strip()
except Exception as e:
print(e)
product_extra_info = 'no details'
print(title.text.strip(), price.text.strip())
print(product_extra_info)
close_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[]'))).find_element(By.CSS_SELECTOR, 'button[]')
close_button.click()
print('closed extra info popup, moving to the next element')
t.sleep(2)
print('_________________________')
items_list.append((title.text.strip(), price.text.strip()))
df = pd.DataFrame(items_list, columns = ['Item', 'Price'])
print(df)
這將在終端列印:
accepted cookies
10 total items found
20 total items found
30 total items found
40 total items found
50 total items found
60 total items found
70 total items found
80 total items found
90 total items found
100 total items found
110 total items found
more than 100 found, stopping
Edan M3A Vital Signs Monitors $3,016.95
M3A Vital Signs Monitor/Meter with lightweight, portable design provides essential measurement of patient's vital signs
Suitable for adult, pediatric and neonatal patients
Features automatic, manual, continuous and average BP modes
Includes high-resolution LED screen (6"), real-time measurements and trend display, and user-friendly interface
2-year manufacturer's warranty for monitor; 1-year for accessories
closed extra info popup, moving to the next element
_________________________
M3 Vital Signs Monitors by Edan Instruments $2,783.95
M3 Vital Signs Monitors/Meters provide essential measurement of patients' vital signs
Lightweight, portable design includes high-resolution LED screen (6")
User-friendly interface offers real-time measurements and trend display
Auto, manual, continuous and average BP modes
If purchased in conjunction with CQCMDLN0002, the CareConnection vitals integration solution, the M3 will allow vitals to pass to the resident record within the EMR. For more information, please email careconnection@medline, or reach out to your local Medline sales representative.
closed extra info popup, moving to the next element
_________________________
Vital Signs Patient Monitors - Touch Screen $2,015.95
[....]
基于這些答案,您現在應該能夠以您需要的形式獲得所需的資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507727.html
標籤:javascript Python 硒 硒网络驱动程序
上一篇:如何匹配字串并點擊seleniumwebdriver
下一篇:發現結束python行程的問題
