使用请求选择和上传 pdf 文件时遇到问题 [英] Trouble choosing and uploading a pdf file using requests
问题描述
我正在尝试使用 post http 请求在 python 中创建一个脚本来上传这个 pdf 文件在网页中.我试过如下,但不幸的是,脚本无法上传文件.
这是登录链接.这是用户名 SmthShift_123
和密码 7/B!yzRd8wuK!N2
供您考虑.现在转到此页面并点击最后一个标签Anhang
在这里你可以找到上传选项.
为了让您可视化 - 这个是那个页面的样子.
这是我目前的尝试:
导入请求从 bs4 导入 BeautifulSouplogin_url = 'https://jobs.commerzbank.com/index.php?ac=login'application_link = 'https://jobs.commerzbank.com/index.php?ac=application&jobad_id=30670'target_link = 'https://jobs.commerzbank.com/index.php?ac=application&page=6'upload_link = 'https://jobs.commerzbank.com/inc/candidate_attachments.php'使用 requests.Session() 作为 s:s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'res = s.get(login_url)酱 = BeautifulSoup(res.text,"lxml")elem = {i['name']:i.get('value','') for i in sauce.select('input[name]')}elem['用户名'] = 'SmthShift_123'elem['密码'] = '7/B!yzRd8wuK!N2's.post(login_url,data=elem)s.get(application_link)resp = s.get(target_link)汤 = BeautifulSoup(resp.text,"lxml")payload = {i['name']:i.get('value','') for i in soup.select('input[name]')}有效载荷['form-control'] = 'Anschreiben'payload['upload'] = 'Datei hochladen'有效载荷['保存'] = ''文件 = {'searchButton': open('CV.pdf','rb')}s.post(upload_link,files=files,data=payload)
当我执行上述脚本时,它既不保存该文件也不抛出任何错误.
我也这样尝试过(仅使用 selenium 进行上传)但脚本也无法选择和上传文件:
s.post(login_url,data=elem)s.get(application_link)resp = s.get(target_link)驱动程序 = webdriver.Chrome()driver.get(resp.url)driver.delete_all_cookies()对于 s.cookies.items() 中的 cookie:driver.add_cookie({"name": cookie[0], "value": cookie[1]})driver.get(resp.url)select = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "select#upload_category"))))select.select_by_visible_text("Lebenslauf")WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#upload_file"))).send_keys("C://Users/WCS/Desktop/CV.pdf")
<块引用>
如何使用请求选择和上传pdf文件?
我可以使用 selenium 上传它.这个网站很棘手.它有一个隐藏的 input
,只有在鼠标悬停上传按钮时才会出现.
试试这个:
from selenium import webdriver从时间导入睡眠from selenium.webdriver.common.by import By从 selenium.webdriver.support.select 导入选择从 selenium.webdriver.support.wait 导入 WebDriverWait从 selenium.webdriver.support 导入 expected_conditions 作为 EC从 selenium.webdriver.common.action_chains 导入 ActionChainslogin_url = 'https://jobs.commerzbank.com/index.php?ac=login'驱动程序 = webdriver.Chrome()driver.implicitly_wait(5)driver.maximize_window()driver.get(login_url)driver.find_element(By.ID, 'nav_login').click()driver.find_element(By.CSS_SELECTOR, 'div.popover-content #quick-login-username').send_keys('SmthShift_123')driver.find_element(By.CSS_SELECTOR, 'div.popover-content #quick-login-password').send_keys('7/B!yzRd8wuK!N2')driver.find_element(By.CSS_SELECTOR,"div.popover-content #quick_login_form button[type='submit']").click()driver.get('https://jobs.commerzbank.com/index.php?ac=application&jobad_id=30670')driver.find_element(By.CSS_SELECTOR, ".applicationform-tab[data-pagenumber='6']").click()选择 = 选择(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "select#upload_category"))))select.select_by_visible_text("Lebenslauf")WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#upload_file")))e = driver.find_element(By.CSS_SELECTOR, "input#upload_file")# 悬停并等待工具提示出现动作 = 动作链(驱动程序)action.move_to_element_with_offset(e, 5, 5)动作.执行()睡觉(3)# 现在,让我们搜索隐藏的输入并发送密钥e = driver.find_element(By.CSS_SELECTOR, "input[name='attachment']")e.send_keys("//CV.pdf")# 并点击上传:e = driver.find_element(By.CSS_SELECTOR, "input#start_file_upload_button")e.click()
希望,这也适用于您.祝你好运!
I'm trying to create a script in python using post http request to upload this pdf file in a webpage. I've tried like the following but unfortunately, the script could not upload the file.
This is the log-in link. Here are the username SmthShift_123
and password 7/B!yzRd8wuK!N2
for your consideration. Now go to this page and click on the last tab Anhang
where you will find the upload option.
To let you visualize - this is how that page looks like.
This is my try so far:
import requests
from bs4 import BeautifulSoup
login_url = 'https://jobs.commerzbank.com/index.php?ac=login'
application_link = 'https://jobs.commerzbank.com/index.php?ac=application&jobad_id=30670'
target_link = 'https://jobs.commerzbank.com/index.php?ac=application&page=6'
upload_link = 'https://jobs.commerzbank.com/inc/candidate_attachments.php'
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
res = s.get(login_url)
sauce = BeautifulSoup(res.text,"lxml")
elem = {i['name']:i.get('value','') for i in sauce.select('input[name]')}
elem['username'] = 'SmthShift_123'
elem['password'] = '7/B!yzRd8wuK!N2'
s.post(login_url,data=elem)
s.get(application_link)
resp = s.get(target_link)
soup = BeautifulSoup(resp.text,"lxml")
payload = {i['name']:i.get('value','') for i in soup.select('input[name]')}
payload['form-control'] = 'Anschreiben'
payload['upload'] = 'Datei hochladen'
payload['save'] = ''
files = {
'searchButton': open('CV.pdf','rb')
}
s.post(upload_link,files=files,data=payload)
When I execute the above script, it neither saves that file nor throws any error.
I also tried like this (using selenium only to do the uploading) but the script can't choose and upload the file either:
s.post(login_url,data=elem)
s.get(application_link)
resp = s.get(target_link)
driver = webdriver.Chrome()
driver.get(resp.url)
driver.delete_all_cookies()
for cookie in s.cookies.items():
driver.add_cookie({"name": cookie[0], "value": cookie[1]})
driver.get(resp.url)
select = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "select#upload_category"))))
select.select_by_visible_text("Lebenslauf")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#upload_file"))).send_keys("C://Users/WCS/Desktop/CV.pdf")
How can I choose and upload the pdf file using requests?
I could upload it using selenium. This website is tricky. It has a hidden input
that only appears upon hovering a button to upload.
Try this:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
login_url = 'https://jobs.commerzbank.com/index.php?ac=login'
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.maximize_window()
driver.get(login_url)
driver.find_element(By.ID, 'nav_login').click()
driver.find_element(By.CSS_SELECTOR, 'div.popover-content #quick-login-username').send_keys('SmthShift_123')
driver.find_element(By.CSS_SELECTOR, 'div.popover-content #quick-login-password').send_keys('7/B!yzRd8wuK!N2')
driver.find_element(By.CSS_SELECTOR,"div.popover-content #quick_login_form button[type='submit']").click()
driver.get('https://jobs.commerzbank.com/index.php?ac=application&jobad_id=30670')
driver.find_element(By.CSS_SELECTOR, ".applicationform-tab[data-pagenumber='6']").click()
select = Select(
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "select#upload_category"))))
select.select_by_visible_text("Lebenslauf")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#upload_file")))
e = driver.find_element(By.CSS_SELECTOR, "input#upload_file")
# Hover over and wait for tooltip to appear
action = ActionChains(driver)
action.move_to_element_with_offset(e, 5, 5)
action.perform()
sleep(3)
# Now, let's search for a hidden input and send keys
e = driver.find_element(By.CSS_SELECTOR, "input[name='attachment']")
e.send_keys("/<path>/CV.pdf")
# And click to upload:
e = driver.find_element(By.CSS_SELECTOR, "input#start_file_upload_button")
e.click()
Hope, this will work for you as well. Good luck!
这篇关于使用请求选择和上传 pdf 文件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!