开发者社区> 问答> 正文

使用请求无法选择和上传pdf文件

我正在尝试使用post http请求在python中创建脚本,以在网页中上传此* pdf *文件。我已经尝试过以下操作,但是很遗憾,脚本无法上传文件。

这是登录链接。这是用户名* SmthShift_123 和密码 7 / B!yzRd8wuK!N2 *供您考虑。现在转到此页面,然后单击最后一个选项卡Anhang,您将在其中找到上载选项。

为了让您形象化-* this *是该页面的外观。

到目前为止,这是我的尝试:

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)

当我执行上述脚本时,它既不保存该文件也不引发任何错误。

我也这样尝试过(仅使用硒进行上传),但脚本也无法选择并上传文件:

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")

如何使用请求选择和上传pdf文件?

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 22:45:46 1132 0
1 条回答
写回答
取消 提交回答
  • 我可以使用硒上传。这个网站很棘手。它具有一个隐藏的“输入”输入,仅在将按钮悬停以上传时才会显示。

    尝试这个:

    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()
    

    希望这也会为您服务。祝好运!

    回答来源:stackoverflow

    2020-03-24 22:45:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
对象存储实战指南-试读 立即下载
OpenAnolis 龙蜥操作系统开源社区技术创新白皮书 立即下载
使用CNFS搭建弹性Web服务 立即下载