如何使用python将文件夹中的所有图像上传到网站

发布于 2025-01-09 18:01:00 字数 2662 浏览 0 评论 0原文

我试图从具有“.jpg”的文件夹中获取所有图像并将其上传到网站上的元素。一般来说,我对编码非常陌生,希望有人能指出我正确的方向。在函数 upload_image 中,我尝试将所有图像上传到文件变量。有人可以与我分享如何执行此操作吗?

import textwrap
import os
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import csv
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui
import time
import codecs
from PIL import Image
from selenium.common.exceptions import NoSuchElementException

opts = Options()
opts.add_experimental_option('debuggerAddress', 'localhost:3333')
driver = webdriver.Chrome(executable_path="C:\chromedriver_win32\chromedriver.exe", chrome_options=opts)
act = ActionChains(driver)

product_data_upload = '/Users/singh/PycharmProjects/ebayscraper/product_url_tables_shpock.csv'
images_folder = '/Users/singh/PycharmProjects/ebayscraper/images_folder'

types_of_encoding = ["utf8", "cp1252"]
for encoding_type in types_of_encoding:
    with codecs.open(product_data_upload, 'r', encoding=encoding_type, errors='replace') as csv_file:
        csv_reader = csv.reader(csv_file)

        next(csv_reader)

        for line in csv_reader:
            driver.implicitly_wait(5)
            driver.get('https://www.shpock.com/en-gb/sell')

            def image_link_extraction():
                os.chdir(images_folder)
                driver.execute_script('window.open("https://www.google.com/");')
                driver.switch_to.window(driver.window_handles[1])
                driver.get(line[0])

                pyautogui.moveTo(373, 896, 0.1)
                pyautogui.click()

                for i in range(1,6):
                    image_url = driver.find_element_by_xpath('//img[contains(@id, "viEnlargeImgLayer_img_ctr")]')
                    name = image_url.get_attribute('alt')
                    url = image_url.get_attribute('src')
                    pyautogui.moveTo(1866, 499, 0.1)
                    pyautogui.click()
                    with open(name.replace(' ', '-').replace('/', '') + '.jpg', 'wb') as f:
                        im = requests.get(url)
                        f.write(im.content)
                        print('writing: ', name)

                    print(name, url)

                driver.close()
                driver.switch_to.window(driver.window_handles[0])

                time.sleep(10)

            image_link_extraction()

            def upload_image():
                file = driver.find_elements_by_xpath('//input[contains(@type, "file")]')
                os.startfile('/Users/singh/PycharmProjects/ebayscraper/images_folder/*.jpg', )

            upload_image()

I am trying to get all images from a folder which has an ".jpg" and upload it to an element on a website. I am very new to coding in general and would appreciate it someone could point me into the right direction. In the function upload_image I am trying to upload all the images to the file variable. Could any share with me how I can perform this?

import textwrap
import os
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import csv
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui
import time
import codecs
from PIL import Image
from selenium.common.exceptions import NoSuchElementException

opts = Options()
opts.add_experimental_option('debuggerAddress', 'localhost:3333')
driver = webdriver.Chrome(executable_path="C:\chromedriver_win32\chromedriver.exe", chrome_options=opts)
act = ActionChains(driver)

product_data_upload = '/Users/singh/PycharmProjects/ebayscraper/product_url_tables_shpock.csv'
images_folder = '/Users/singh/PycharmProjects/ebayscraper/images_folder'

types_of_encoding = ["utf8", "cp1252"]
for encoding_type in types_of_encoding:
    with codecs.open(product_data_upload, 'r', encoding=encoding_type, errors='replace') as csv_file:
        csv_reader = csv.reader(csv_file)

        next(csv_reader)

        for line in csv_reader:
            driver.implicitly_wait(5)
            driver.get('https://www.shpock.com/en-gb/sell')

            def image_link_extraction():
                os.chdir(images_folder)
                driver.execute_script('window.open("https://www.google.com/");')
                driver.switch_to.window(driver.window_handles[1])
                driver.get(line[0])

                pyautogui.moveTo(373, 896, 0.1)
                pyautogui.click()

                for i in range(1,6):
                    image_url = driver.find_element_by_xpath('//img[contains(@id, "viEnlargeImgLayer_img_ctr")]')
                    name = image_url.get_attribute('alt')
                    url = image_url.get_attribute('src')
                    pyautogui.moveTo(1866, 499, 0.1)
                    pyautogui.click()
                    with open(name.replace(' ', '-').replace('/', '') + '.jpg', 'wb') as f:
                        im = requests.get(url)
                        f.write(im.content)
                        print('writing: ', name)

                    print(name, url)

                driver.close()
                driver.switch_to.window(driver.window_handles[0])

                time.sleep(10)

            image_link_extraction()

            def upload_image():
                file = driver.find_elements_by_xpath('//input[contains(@type, "file")]')
                os.startfile('/Users/singh/PycharmProjects/ebayscraper/images_folder/*.jpg', )

            upload_image()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文