在python中使用urllib3传递用户名和密码

在python中使用urllib3传递用户名和密码,python,urllib3,Python,Urllib3,我正在尝试从下一页获取html响应 当我在chrome中打开这个url时,我必须输入用户名和我在该网站上的帐户密码 我想在python中使用urllib3传递这个用户名和密码,我当前的代码是 import urllib3 url = 'https://ghrc.nsstc.nasa.gov/pub/lis/iss/data/science/nqc/nc/2020/0101/' username = '' password = '' data = {'Username': username,

我正在尝试从下一页获取html响应

当我在chrome中打开这个url时,我必须输入用户名和我在该网站上的帐户密码

我想在python中使用urllib3传递这个用户名和密码,我当前的代码是

import urllib3

url = 'https://ghrc.nsstc.nasa.gov/pub/lis/iss/data/science/nqc/nc/2020/0101/'
username = ''
password = ''
data = {'Username': username, 'Password': password}

http = urllib3.PoolManager()
r = http.request('POST', url, data)
print(r.status)

print(r.data)
但是,运行此命令仍然会给出登录页面的响应


我不确定我是否需要使用cookies,或者如何确定用户名和密码需要以何种格式传递到url才能成功登录,并被带到指定的url上。至少对我来说,单纯的POST请求很难做到这一点。对于这样的项目,我会使用Selenium

pip install selenium
从此处下载Chrome驱动程序:

从下载的文件中,将chromedriver.exe文件复制到应用程序根目录

这是要登录的代码


也许这两个问题对你有用?这能回答你的问题吗?另一方面,为什么直接使用urllib而不是像请求这样的东西呢?@matteo.rebeschi还有一个问题。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#create an instance of webdriver
driver = webdriver.Chrome()

#navigate to URL
driver.get("https://ghrc.nsstc.nasa.gov/pub/lis/iss/data/science/nqc/nc/2020/0101")

# username and password variable
username = 'my_username'
password = 'my_password'

#get the username and password fields by id and fill them
input_user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'username')))
input_user.send_keys(username)
input_pwd = driver.find_element_by_id('password')
input_pwd.send_keys(password)
#click the login button
btn = driver.find_element_by_xpath('//input[@type="submit"]')
btn.click()