from selenium import webdriver
import os
import time
from fake_useragent import UserAgent
import requests
from loguru import logger
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui
ua = UserAgent(verify_ssl=False)
headers = {
"Cookie": "_ga=GA1.2.929478863.1645602135; _gid=GA1.2.114174532.1646029429; Hm_lvt_768a5f0e1e2da152800f053cec2f560a=1645602133,1646029427,1646099330; Hm_lpvt_768a5f0e1e2da152800f053cec2f560a=1646101802"
,
"User-Agent": ua.random
}
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
root = os.getcwd()
@logger.catch()
def get_img_data(url, num, code):
driver.get(url)
driver.implicitly_wait(2)
img_code = driver.current_url.split("/")[-1]
img_name = driver.find_element_by_xpath("/html/body/section[1]/div/main/article/h1").text
os.chdir(root)
os_path_file = f"{img_code}_{img_name}"
if not os.path.exists(os_path_file):
os.mkdir(f"{os_path_file}")
os.chdir(f"{os_path_file}")
img_src = driver.find_element_by_xpath("/html/body/section[1]/div/main/article/figure/img").get_attribute('src')
logger.info(f"{img_src}")
img_end = driver.find_element_by_xpath("/html/body/section[1]/div/main/div[1]/div[2]/div/div[2]").text
logger.info(f"{img_end}")
if img_end != '没有了':
logger.info("还有")
newwindow = f'window.open("{img_src}")'
driver.execute_script(newwindow)
driver.switch_to_window(driver.window_handles[1])
pic = driver.find_element_by_xpath("/html/body/img")
action = ActionChains(driver).move_to_element(pic)
action.context_click(pic)
action.perform()
pyautogui.typewrite(['v'])
time.sleep(1)
pyautogui.typewrite(['enter'])
@logger.catch()
def run():
url = '***'
get_img_data(url, 0, 138)
if __name__ == '__main__':
run()
|