Python机械化表单身份验证错误

时间:2013-08-29 21:26:26

标签: python authentication forms-authentication mechanize

我正在尝试在Sharepoint网站上自动填写表单,但是当我从下面输入网址时,我的Python脚本无法通过此弹出的身份验证框。

Auth Box

from base64 import b64encode
import mechanize

url = 'http://moss.micron.com/MFG/ProbeTest/Lists/Manufacturing%20Requests/AllItems.aspx'
username = 'username'
password = 'password'

# I have had to add a carriage return ('%s:%s\n'), but
# you may not have to.
b64login = b64encode('%s:%s' % (username, password))

br = mechanize.Browser()

br.addheaders.append( 
  ('Authorization', 'Basic %s' % b64login,  )
)

br.open(url)!

这会导致以下错误:

Error

修改

以下是在请求的页面上运行wget的结果。

--2013-08-30 11:16:17--  http://moss.micron.com/MFG/ProbeTest/Lists/Manufacturing%20Requests/AllItems.aspx
Resolving moss.micron.com... 137.201.88.118
Connecting to moss.micron.com|137.201.88.118|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 401 Unauthorized
  Server: Microsoft-IIS/7.0
  WWW-Authenticate: Negotiate
  WWW-Authenticate: NTLM
  X-Powered-By: ASP.NET
  MicrosoftSharePointTeamServices: 12.0.0.6341
  Date: Fri, 30 Aug 2013 17:16:17 GMT
  Connection: keep-alive
  Content-Length: 0
Authorization failed.

2 个答案:

答案 0 :(得分:1)

您的浏览器尊重您网站上的robots.txt,禁止它。

在通过以下方式发出请求之前,您可以将mechanize.Browser设置为忽略robots.txt: br.set_handle_robots(False)

或者,编辑您的robots.txt以允许这种连接。

如果在mechanize.Browser中设置自定义UserAgent标头,则可以对其进行过滤。

有关robots.txt的基本信息,请参阅here

答案 1 :(得分:0)

如果您可以使用PC访问该网站,请下载Fiddler2,以便您在登录时查看所需的交易。

编辑..好的。显然你有一台电脑。