[英]Why do I get product details ONLY from the first web page instead of all the 25pages
我有一個 python 腳本,我分成了不同的功能。 我的第一個 function 'get_url' 應該在用戶在提示中輸入所需的產品名稱后獲得產品 url 。 我想在網站的所有頁面中獲取產品詳細信息。 現在,當我運行我的代碼時,我只能從第一個 web 頁而不是所有 25 頁中獲取產品詳細信息。 請幫助@Nathan Mills
這是我的代碼塊
from selenium import webdriver
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pandas as pd
def get_url(product):
for x in range(1, 26):
product = product.replace(' ', '%20')
template = 'https://www.konga.com/search?search=={product}&page=={x}'
url = template.format(product, x)
return url
def get_all_products(nest):
name = nest.find_element(By.CLASS_NAME, 'af885_1iPzH').text.strip()
current_price = nest.find_element(By.CLASS_NAME, 'd7c0f_sJAqi').text.strip()
reviews = nest.find_element(By.CLASS_NAME, 'eea9b_1Ma8-').text.strip()
product_info = (name, current_price, reviews)
return product_info
def main(product):
product_list = []
url = get_url(product)
service = Service(executable_path="C:/driver/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get(url)
driver.maximize_window()
time.sleep(5)
product_cards = driver.find_elements(By.CLASS_NAME, 'bbe45_3oExY')
time.sleep(5)
for everyCard in product_cards:
productDetails = get_all_products(everyCard)
product_list.append(productDetails)
col = ['Product_Name', 'Current_Price', 'Product_Reviews']
df = pd.DataFrame(product_list, columns=col)
df.to_csv("C:\\Users\LP\Documents\MainTest\MainTest.csv", index=False, encoding='utf-8', mode='a')
product = input('Enter Product You Are Looking For : ')
main(product)
function get_url()
有一個以 return 結束的循環。 因此 function 在第一個周期后停止工作。 它生成一個 URL,返回它,僅此而已。
要解決此問題,您可以將所有 url 收集到 function 內的一個變量中並返回它或將return
更改為yield
。 這個 yield 會將你的 function 變成一個生成器,你可以將它用作一個列表。
此外,我將行url = template.format(product, x)
更改為url = template.format(product=product, x=x)
因為它不起作用。
所以帶有yield
的代碼如下:
from selenium import webdriver
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pandas as pd
def get_url(product):
for x in range(1, 26):
product = product.replace(' ', '%20')
template = 'https://www.konga.com/search?search={product}&page={x}'
url = template.format(product=product, x=x)
yield url
def get_all_products(nest):
name = nest.find_element(By.CLASS_NAME, 'af885_1iPzH').text.strip()
current_price = nest.find_element(By.CLASS_NAME, 'd7c0f_sJAqi').text.strip()
reviews = nest.find_element(By.CLASS_NAME, 'eea9b_1Ma8-').text.strip()
product_info = (name, current_price, reviews)
return product_info
def main(product):
product_list = []
url = get_url(product)
for one_url in url:
service = Service(executable_path="C:/driver/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get(one_url)
driver.maximize_window()
time.sleep(5)
product_cards = driver.find_elements(By.CLASS_NAME, 'bbe45_3oExY')
time.sleep(5)
for everyCard in product_cards:
productDetails = get_all_products(everyCard)
product_list.append(productDetails)
col = ['Product_Name', 'Current_Price', 'Product_Reviews']
df = pd.DataFrame(product_list, columns=col)
df.to_csv("C:\\Users\LP\Documents\MainTest\MainTest.csv", index=False, encoding='utf-8', mode='a')
driver.quit()
product = input('Enter Product You Are Looking For : ')
main(product)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.