使用Python向ASPX页面提交请求时出错

时间:2014-07-31 03:33:13

标签: python asp.net

我正在用Python试验我的技能,我尝试在python中编写一个小脚本,它将使用选项" network whois record&#34发送请求以找出该网站所在的数据中心。 ;

因此我使用了以下网站:

http://centralops.net/co/DomainDossier.aspx

我写了以下脚本:

#!/usr/bin/env python
import urllib2
import urllib

url = "http://centralops.net/co/DomainDossier.aspx"
headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
                'Accept': 'text/html,application/xhtml+xml,application/xml; q=0.9,image/webp,*/*; q=0.8',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Connection' : 'keep-alaive',
                'Accept-Encoding': 'none',
                'Accent-Language':'en-US,en;q=0.8',
                'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
                }
        # All taken from the source page of the website.
FormFields = ((r'_VIEWSTATE',''),(r'addr',r'yahoo.com'),(r'dom_whois','off'),(r'dom_dns','off'),(r'traceroute','off'),(r'net_whois','on'),(r'svc_scan','off'))

encodedFields = urllib.urlencode(FormFields)
req = urllib2.Request(url,encodedFields,headers)
f = urllib2.urlopen(req)

但问题来自于我尝试执行该文件时出现以下错误:

Traceback (most recent call last):   File "data_center.py", line 18,
in <module>
    f = urllib2.urlopen(req)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 127, in urlopen
    return _opener.open(url, data, timeout)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 410, in open
    response = meth(req, response)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 523, in http_response
    'http', request, response, code, msg, hdrs)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 448, in error
    return self._call_chain(*args)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 382, in _call_chain
    result = func(*args)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 403: Access denie

所以,请问,有人可以帮我解决这个问题吗?

提前多多感谢。

1 个答案:

答案 0 :(得分:1)

正如Flight Odyssey所暗示他们可能会阻止自动请求。

更改您的代码,以便打印网址而不是提交,并尝试手动粘贴以确保您转到正确的页面 - 可能还有其他步骤/验证码等。

另外,不确定是否是拼写错误,但标题是

  'Connection' : 'keep-alaive',