我是 Python 的新手。我想在 python 中编写一个脚本,在其中运行数千个 URL 并保存响应。为了访问这些 URL,需要凭据。因此,我编写了一个基本脚本,该脚本转到 URL 并打印响应。当我通过多个 URL 时,网站返回多个用户登录的错误。所以,我想登录一次并在同一个登录会话中运行其他 URL。有什么办法可以用机械汤做到这一点。
这是我的脚本:
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("mywebsite1")
browser.select_form('form[action="/login"]')
browser.get_current_form().print_summary()
browser["userId"] = "myusername"
browser["password"] = "mypassword"
response = browser.submit_selected()
browser.launch_browser()
print(response.text)
browser.open("mywebsite2")
print(response.text)
...... so on for all the URLs
如何保存我的会话?在此先感谢您的帮助