因此,我一直在尝试使用python创建网站的文件上传脚本,但未上传文件。这是代码-
from splinter import Browser
import random, string
import sys
uploadurl = "https://shareoverair.com/upload"
randstring = ''.join(random.choices(string.ascii_letters + string.digits,
k=6))
browser = Browser('chrome', headless=True)
browser.visit(uploadurl)
browser.fill('fileid', randstring)
browser.attach_file('fileupload', str(sys.argv[1]))
lul = browser.find_by_css(".btn")
for i in range(0, len(lul)-1):
lul[i].click
i += 1
print(browser.url)
print(randstring)
for循环仅用于我的测试。我确保单击每个按钮。我什至尝试单击按钮1比1,但没有运气。 这是我要上传到的网站- https://shareoverair.com/upload
这是上传表单-
<form id="upload-form" enctype="multipart/form-data" method="post" novalidate="novalidate">
<div class="alert bg--error hide" id="upload-error">
<div class="alert__body">
<span></span>
</div>
<div class="alert__close">
×
</div>
</div>
<div class="upload-input input-text">
<label>Choose a unique File ID:</label>
<input type="text" name="fileid" id="fileid" placeholder="File ID">
<a class="generate-file-id" href="javascript:void(0);" style="float: right;">Generate?</a>
</div>
<div class="upload-input input-text upload-method-file">
<label>Select your file:</label>
<label for="fileupload" class="file-choser">Choose File</label>
<input type="file" id="fileupload" name="fileupload" style="display: none;">
</div>
<div class="upload-input upload-password input-text hide">
<label>Choose a Password:</label>
<input type="password" name="password" id="password" placeholder="Password">
</div>
<button class="btn btn--primary type--uppercase upload-button" type="submit">Upload Now</button>
<div class="file-upload-progress progress-horizontal progress-horizontal--lg hide">
<div class="progress-horizontal__bar upload-progressbar" data-value="0"><div class="progress-horizontal__progress" style="width: 0%;"></div></div>
<span class="progress-horizontal__label h5 upload-status">Progress: 0%</span>
</div>
</form>
我尝试使用请求代替。这是代码-
import requests
import random, string
import sys
uploadurl = "https://shareoverair.com/upload"
filestring = ''.join(random.choices(string.ascii_letters + string.digits, k=5))
fileid = {'fileid': filestring}
file = {'fileupload': open(str(sys.argv[1]), 'rb')}
headers = {'User-Agent': 'Mozilla/5.0'}
upload = requests.post(uploadurl, headers=headers, files=file, data=fileid)
print(upload.text)
print(filestring)
PS-代码确实执行!
我也尝试将文本搜索与“立即上传”一起使用,但是排除了索引异常
编辑- 我也尝试过使用硒,但是没有运气
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import random, string
import sys
uploadurl = "https://shareoverair.com/upload"
randstring = ''.join(random.choices(string.ascii_letters + string.digits,
k=6))
options = Options()
options.headless = True
browser = webdriver.Chrome(options=options)
browser.get(uploadurl)
fild = browser.find_element_by_id('fileid')
fild.send_keys(randstring)
uplfile = browser.find_element_by_name('fileupload')
uplfile.send_keys(sys.argv[1])
upload = browser.find_element_by_xpath("//button[contains(text(), 'Upload Now')]")
print(upload)
upload.click()
print(browser.current_url)
print(randstring)