迷失的小屋
首页
注册

[Chromediver]2025/5/19上课存档

迷失的蒙娜丽莎
迷失的蒙娜丽莎
2025-05-16 11:32:49
import os

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import sys
from bs4 import BeautifulSoup
import requests
import random

chrome_driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
# # 创建 Service 对象
service = Service(chrome_driver_path)
# # 启动 WebDriver
driver = webdriver.Chrome(service=service)
#driver = webdriver.Chrome()
driver.get('https://www.1688.com/zw/page.html?hpageId=old-sem-pc-list&keywords=%E6%89%8B%E6%9C%BA%E5%A3%B3')
driver.maximize_window()
bs4_item = BeautifulSoup(driver.page_source, 'html.parser')
#next_button=bs4_item.find('button',class_='next-btn next-btn-normal next-btn-large next-pagination-item next')
element = driver.find_element(By.XPATH,'/html/body/div[1]/div/div/div[4]/div/div/div[2]/div/div/button[2]')
text_item = bs4_item.find('span', class_='next-pagination-display')
iteritor_number=int(text_item.text[-2::])
print('一共有'+str(iteritor_number)+'页')

os.makedirs('save')

for i in range(iteritor_number):
    print('\n现在是第'+str(i+1)+'页\n')
    bs4_item = BeautifulSoup(driver.page_source, 'html.parser')
    cont_div = bs4_item.find_all('div', class_='offer_exp')
    for cont_dive_item in cont_div:
        item1=cont_dive_item.find('div',class_='new_ui_offer offer_item')
        text=item1.find('div',class_='offer-title').text
        img=item1.find('div',class_='offer-img')
        price=item1.find('div',class_='offer-price')
        price1=price.find('span',class_='solt solt-r').text
        item_price=price.find('span',class_='price').text
        img1=img.find('img').get('src')
        print(text,img1,price1,item_price)
        time.sleep(0.1)
        dir_1='save/'+price1+item_price
        os.makedirs(dir_1)
        with open (dir_1+'/'+ text.replace('/','_') + '.png', 'wb') as pngwriter:
            pngwriter.write(requests.get(img1).content)
            print(text,'下载成功')
        time.sleep(random.randint(2,4))
    element.click()
    time.sleep(random.randint(4,7))
print(i)