提交POST请求到aspx页面 [英] Submitting a post request to an aspx page

查看:1116
本文介绍了提交POST请求到aspx页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在<一个ASPX页href=\"https://searchlight.cluen.com/E5/CandidateSearch.aspx\">https://searchlight.cluen.com/E5/CandidateSearch.aspx上有一个形式,我想提交和分析的信息。

使用Python的urllib而urllib2的我创建了正确的头和用户代理POST请求。但所得的HTML响应不包含结果的预期表。我误解还是我失去了任何明显的细节?

 进口的urllib
    进口的urllib2    标题= {
        HTTP_USER_AGENT:Mozilla的/ 5.0(视窗; U; Windows NT的5.1; EN-US; RV:1.9.0.13)的Gecko / 2009073022火狐/ 3.0.13,
        HTTP_ACCEPT:text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9,* / *; Q = 0.8',
        内容类型:应用程序/ x-WWW的形式urlen codeD
    }
    #无法查看https://searchlight.cluen.com/E5/CandidateSearch.aspx源获得的这些值
    视图状态='/ wEPDwULLTE3NTc4MzQwNDIPZBYCAg ... uJRWDs / 6Ks1FECco ='
    eventvalidation ='/ wEWjQMC8pat6g4C77jgxg0CzoqI8wgC3uWinQQCwr / ... oPKYVeb74 ='
    URL ='https://searchlight.cluen.com/E5/CandidateSearch.aspx
    FORMDATA =(
        (__VIEWSTATE,视图状态),
        (__EVENTVALIDATION',eventvalidation)
        ('__EVENTTARGET',''),
        ('__EVENTARGUMENT',''),
        ('textcity',''),
        ('dropdownlistposition',''),
        ('dropdownlistdepartment',''),
        ('dropdownlistorderby',''),
        ('TEXTSEARCH',''),
    )    #更改用户代理
    从进口的urllib FancyURLopener
    类MyOpener(FancyURLopener):
        版本='的Mozilla / 5.0(视窗; U; Windows NT的5.1;它; RV:1.8.1.11)的Gecko / 20071127火狐/ 2.0.0.11    myopener = MyOpener()    #恩code的请求后格式的表单数据
    EN codedFields = urllib.urlen code(FORMDATA)    F = myopener.open(URL,连接codedFields)
    打印f.info()    尝试:
      FOUT =打开('tmp.htm','W')
    除:
      打印(无法打开输出文件\\ n)    fout.writelines(f.readlines())
    fout.close()

有关于这个话题,是有帮助的(如的如何提交查询的.aspx在python 页),但我卡在这一点,并寻求更多的帮助,如果这是可能的。

生成的HTML页面是说我可能需要登录,但aspx页面显示在浏览器中没有任何登录。

下面是从信息()的结果:


  

连接:关闭
  日期:星期二,2011年6月7日17时05分26秒GMT
  服务器:Microsoft-IIS / 6.0
  的X已启动方式:ASP.NET
  的X ASPNET-版本:2.0.50727
  缓存控制:私人
  内容类型:text / html的;字符集= UTF-8
  内容长度:1944年



解决方案

我试过机械化和urllib2的,以及机械化处理Cookie更好。我可以简单地用机械化指定提交表单:

 浏览器= mechanize.Browser()
    browser.select_form(FORM_NAME)
    browser.set_value(页面$下一步,NAME =页面编号)

这是没有必要手动复制POST请求,在这种情况下被机械化能够处理依赖于JavaScript的一种形式。

I have an ASPX page at https://searchlight.cluen.com/E5/CandidateSearch.aspx with a form on it, that I'd like to submit and parse for information.

Using Python's urllib and urllib2 I created a post request with the proper headers and user agent. But the resulting html response does not contain the expected table of results. Am I misunderstanding or am I missing any obvious details?

    import urllib
    import urllib2

    headers = {
        'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.13)         Gecko/2009073022 Firefox/3.0.13',
        'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    # obtained these values from viewing the source of https://searchlight.cluen.com/E5/CandidateSearch.aspx
    viewstate = '/wEPDwULLTE3NTc4MzQwNDIPZBYCAg ... uJRWDs/6Ks1FECco='
    eventvalidation = '/wEWjQMC8pat6g4C77jgxg0CzoqI8wgC3uWinQQCwr/ ... oPKYVeb74='
    url = 'https://searchlight.cluen.com/E5/CandidateSearch.aspx'
    formData = (
        ('__VIEWSTATE', viewstate),
        ('__EVENTVALIDATION', eventvalidation),
        ('__EVENTTARGET',''),
        ('__EVENTARGUMENT',''),
        ('textcity',''),
        ('dropdownlistposition',''),
        ('dropdownlistdepartment',''),
        ('dropdownlistorderby',''),
        ('textsearch',''),
    )

    # change user agent
    from urllib import FancyURLopener
    class MyOpener(FancyURLopener):
        version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127         Firefox/2.0.0.11'

    myopener = MyOpener()

    # encode form data in post-request format
    encodedFields = urllib.urlencode(formData)

    f = myopener.open(url, encodedFields)
    print f.info()

    try:
      fout = open('tmp.htm', 'w')
    except:
      print('Could not open output file\n')

    fout.writelines(f.readlines())
    fout.close()

There are several questions on this topic that were helpful (such as how to submit query to .aspx page in python) but I'm stuck on this and asking for additional help, if that is possible.

The resulting html page is saying I may need to log in, but the aspx page displays in my browser without any login.

Here are the results from info():

Connection: close Date: Tue, 07 Jun 2011 17:05:26 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Cache-Control: private Content-Type: text/html; charset=utf-8 Content-Length: 1944

解决方案

I tried mechanize and urllib2, and mechanize handles cookies better. I can submit the form simply by specifying with mechanize:

    browser= mechanize.Browser()
    browser.select_form(form_name)
    browser.set_value("Page$Next", name="pagenumber")     

It was not necessary to replicate the post request manually, and mechanize in this case was able to handle a form that relies on javascript.

这篇关于提交POST请求到aspx页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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