如何通过密码保护的 aspx 网站以编程方式解析它? [英] How can I get past a password protected aspx website in order to parse it programatically?

查看:46
本文介绍了如何通过密码保护的 aspx 网站以编程方式解析它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是表单标记中的操作方法不是 URL,因此 Python 请求模块将无法解析登录之外的 HTML.有问题的网址是 https://www.mms669.org/MMSGB45/default.aspx?ReturnUrl=%2fMMSGB45%2fstudent%2fdefault.aspx

My problem here is that the action method in the form tag is not a URL so the Python requests module won't work in order for me to parse the HTML that is beyond the login. The url in question is https://www.mms669.org/MMSGB45/default.aspx?ReturnUrl=%2fMMSGB45%2fstudent%2fdefault.aspx

推荐答案

使用简单的网页抓取工具很难做到 - 您必须在欺骗登录后捕获并重新发送您的身份验证令牌.刮,值得你花时间使用硒.由于您的问题很笼统,我将给出一个概述.如果您需要具体信息,请告诉我.

Its difficult to do with simple web scraping tools - you have to capture and re-send your authentication tokens after you spoof the log in. For anything but trivial web scraping, it is worth your time to use selenium. Since your question was generic, I'll give an overview. Let me know if you need specifics.

  1. 使用 selenium 创建到网站的连接(我在 gecko driver.)
  2. 导航到登录页面
  3. 使用 selenium 模拟用户输入 uname 和 pw,模拟按下确定"(或其他).
  4. 通过模拟点击链接或直接 url 来导航您下一步要去的地方.由于您使用的是相同的登录连接,因此它可以让您现在到达那里,就像您已登录一样.
  5. 将您的 html 传递给漂亮的汤并刮掉.

这是我的代码示例——希望对您有所帮助.

Here is an example of my code -- hope it helps.

logger.debug(f"{login_info.user_id}: Logging In")

options = webdriver.FirefoxOptions()
if "Linux" in platform.system():
    path = "path/to/geckodriver"
else:
    path = "path/to/geckodriver.exe"

if headless:
    options.add_argument("-headless")
driver = webdriver.Firefox(executable_path=path, firefox_options=options)
driver.implicitly_wait(10)


driver.get(login_info.host)
uname_box = driver.find_element_by_name('txtLoginUserID')
pw_box = driver.find_element_by_name('txtLoginPassword')
login_btn = driver.find_element_by_name('btnLogin')

uname_box.send_keys(login_info.username)
pw_box.send_keys(login_info.password)
time.sleep(1)
login_btn.click()

这篇关于如何通过密码保护的 aspx 网站以编程方式解析它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆