首先,我得到了所有頁面的總 url。但是,當我想進入每個頁面(逐頁)時,它失敗了。我怎么能進入每一頁?
!pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import urllib as url
from urllib.parse import urlparse
import time
browser = webdriver.Chrome(executable_path='./chromedriver.exe')
wait = WebDriverWait(browser,5)
output = []
for i in range(1,2): # Iterate from page 1 to the last page
browser.get("https://tw.mall.yahoo.com/search/product?p=屈臣氏&pg={}".format(i))
# Wait Until the product appear
wait.until(EC.presence_of_element_located((By.XPATH,"//ul[@class='gridList']")))
# Get the products
product_links = browser.find_elements(By.XPATH,"//ul[@class='gridList']/li/a")
# Iterate over 'product_links' to get all the 'href' values
for link in (product_links):
print(f"{link.get_attribute('href')}")
output.append([link.get_attribute('href')])
for b in output:
browser.get(b)
輸出
InvalidArgumentException: Message: invalid argument: 'url' must be a string
(Session info: chrome=96.0.4664.45)
uj5u.com熱心網友回復:
i是型別 int。您需要使用以下函式轉換int為string應用字串插值str():
format(str(i))
解決方案
所以有效的代碼行將是:
for i in range(1,2): # Iterate from page 1 to the last page
browser.get("https://tw.mall.yahoo.com/search/product?p=屈臣氏&pg={}".format(str(i)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/367174.html
下一篇:選擇具有更改xpath的元素
