蝦皮購物 網站爬蟲

透過Selenium和BeautifulSoup爬取蝦皮網站「後背包」搜尋結果,並擷取第一頁所有商品的月銷售量資訊。程式碼示範如何實現自動化搜尋和資料擷取流程。

使用Selenium和BeautifulSoup,搜尋「後背包」,並抓出第一頁所有搜尋結果的月銷售量。

shopee_url = 'https://shopee.tw/'
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(shopee_url)
q = driver.find_element_by_class_name('shopee-searchbar-input__input')
q.send_keys('後背包')
from selenium.webdriver.common.keys import Keys
q.send_keys(Keys.RETURN)
from bs4 import BeautifulSoup
soup = BeautifulSoup(driver.page_source,'lxml')
links = soup.find_all(class_="shopee-search-item-result__item")

for link in links:
   driver.get(shopee_url+link['href'])
   soup_lv2 = BeautifulSoup(driver.page_source,'lxml')
   selling_data = soup_lv2.find_all("div", "shopee-product-info__header__sold-count")
   print(selling_data)
    

🚀 以每年 5 美元解鎖無廣告體驗

✓ 14 天免費試用 ✓ 隨時取消

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *