python - 如何使用 Python Selenium 爬取所有产品?
问题描述
我在 Python Anaconda 中有以下代码来抓取所有 347 项以供我自己练习。但是,它只抓取前 96 个项目。我可以知道如何解决吗?我对动作链做错了吗?
from selenium import webdriver
driver=webdriver.Chrome('C:/Users/cc/Documents/chromedriver.exe')
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
import requests
import ast
from bs4 import BeautifulSoup
user_input=input("input path")
prefix="https://www.toysrus.com.sg/"
url=prefix+user_input
driver.get(url)
response = requests.get(url)
response_text = response.text
soup = BeautifulSoup(response_text, 'lxml')
text = urlopen(url).read()
soup = BeautifulSoup(text)
data = soup.findAll('div',attrs={'class':'card-image-wrapper'})
toc = soup.find_all('div',attrs={'class':'result-count text-center'})
emptylist2=[]
while True:
try:
driver.implicitly_wait(3)
expandable = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".button-class")))
expandables=driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/div[3]/div[2]/div/div[2]/div/div[97]/div[1]/button').click()
for item in expandables:
ActionChains(driver).move_to_element(item).click().perform() # item.click()
except Exception as e:
print(e)
break
for item in toc:
print((item).text.strip()[:-1])
for div in data:
links = div.findAll('a')
for a in links:
catalogueresult=ast.literal_eval("" + a['href'][1:-5][-7:])
print (catalogueresult)
解决方案
推荐阅读
- r - 从邮政编码列表中查找最近的邮政编码
- python - 如何解析一列DataFrame?
- c++ - 是否可以将变体的索引作为 constexpr 变量获取?
- postgresql - 使用在arrac_b 中找不到的array_a 值更新array_c
- shopify - Shopify 中的多个订单
- heroku - 如何暂时停止 Heroku 服务器上的活动以防止收费
- node.js - 如何只允许通过 iframe 访问我的 Web 应用程序中的 1 个 URL?
- c++ - glfw 在创建之前/创建时实例化窗口位置
- dart - 如何在 Dart 中创建静态扩展方法?
- r - 将日期从 JSON 转换为 R 列表元素:“字符串不是标准的明确格式”