我正在开发一个自动完成在线作业的项目。 我可以登录、查找练习,甚至使用 mechanize 填写表格。 我发现提交按钮触发了一个javascript函数,我搜索了解决方案。很多答案都包含“模拟 XHR”。但他们都没有谈论细节。 我不知道这个屏幕截图是否有帮助。 https://i.sstatic.net/0g83g.png 谢谢
如果你想评估 javascript,我建议使用 Selenium。它将打开一个浏览器,然后您可以从 python 向其发送文本。
首先,安装Selenium:https://pypi.python.org/pypi/selenium
然后从这里下载 chrome 驱动程序:https://code.google.com/p/chromedriver/downloads/list
将二进制文件放在与您正在编写的 python 脚本相同的文件夹中。 (或者将其添加到路径或其他任何位置,更多信息请参见:https://code.google.com/p/selenium/wiki/ChromeDriver)
之后以下示例应该可以工作:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()
更多信息这里 (这个例子也来自那里)
xhr 与常规请求相同。以同样的方式进行操作,然后处理响应。