gpt4 book ai didi

Python请求模块不在 session 中传递参数

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:55 25 4
gpt4 key购买 nike

我正在尝试从需要登录身份验证的网站批量下载一系列 PDF。但是,当我尝试对 '/transcripts/transcript.pdf?user_id=3007' 发出 GET 请求时,我能够成功登录,但是该请求返回 '/transcripts 的内容/transcript.pdf'

有人知道为什么 URL 参数没有发送吗?或者为什么要改变路线?

我尝试将参数“user_id”作为数据、参数传递,并在 URL 中硬编码。

出于隐私考虑,我已从下面的字符串中删除了实际域名

with requests.Session() as s:
login = s.get('<domain>/login/canvas')
# print the html returned or something more intelligent to see if it's a successful login page.
print(login.text)
login_html = lxml.html.fromstring(login.text)
hidden_inputs = login_html.xpath(r'//form//input[@type="hidden"]')
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
print("form: ",form)
form['pseudonym_session[unique_id]']= username
form['pseudonym_session[password]']= password
response = s.post('<domain>/login/canvas',data=form)
print(response.url, response.status_code) # gets <domain>?login_success=1 200


# An authorised request.
data = { 'user_id':'3007'}
r = s.get('<domain>/transcripts/transcript.pdf?user_id=3007', data=data)
print(r.url) # gets <domain>/transcripts/transcript.pdf
print(r.status_code) # gets 200
with open('test.pdf', 'wb') as f:
f.write(r.content)

GET 响应返回 /transcripts/transcript.pdf 而不是 /transcripts/transcript.pdf?user_id=3007

最佳答案

从表面上看,您正在尝试使用 Canvas 。我非常确定在 Canvas 中,您可以批量下载所有测试附件。

如果情况并非如此,可以尝试以下几种方法:

  1. 登录后,尝试在浏览器中输入带有 user_id 的 URL。这会将您直接转至 PDF 文件还是指向 PDF 文件的链接?
  2. 如果是的话,看看url,可能根本就没有显示参数;有些网站这样做,不用担心

如果没有,GET 可能还不够;也许该网站使用了 javascript 等。

关于Python请求模块不在 session 中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54371550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com