removebg是世界级的抠图王者,虽提供了api,但是一个月就给50张实在是不够用,价格非常昂贵,如果使用量很大的花是用不起的,原分辨率是很贵,但是预览版(几百*几百)的分辨率是免费在网页端可以使用的,因此使用了python模拟点击。
代码如下:
import os
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from time import sleep
chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
bro=webdriver.Chrome(options=chrome_options, executable_path='./chromedriver')
url='https://www.remove.bg/zh/upload'
bro.get(url=url)
sleep(5)
def exe_human():
sleep(10)
def exe_generate():
sleep(10)
bro.find_element_by_xpath("//*[@id='page-content']/div[2]/div/div/div/div[2]/div[2]/div[2]/button").click()
if __name__ == '__main__':
with open('待处理的图片urls.txt','r') as f:
data=f.readlines()
print('--------开始处理removebg---------')
for index, i in enumerate(data[:]):
sleep(1)
bro.execute_script("uploadUrl('{}');".format(i.strip()))
sleep(15)
try:
iframe = bro.find_element_by_xpath('//*[@id="page"]/div/div[3]/div/div/iframe')
bro.switch_to.frame(iframe)
while True:
try:
bro.find_element_by_xpath("//*[@id='checkbox']")
print('人机验证开始')
sleep(1)
except NoSuchElementException:
print('不存在人机验证')
break # 跳出 while 循环
bro.switch_to.default_content()
exe_generate()
except Exception as e:
exe_generate()
运行就行了,不过前提需要准备好图片的url链接,并且电脑需要安装谷歌浏览器,并且下载对应的chromedriver