使用Python请求向asp.net表单发布不成功

时间:2016-01-13 04:48:59

标签: python asp.net

我正在尝试使用Python请求将值发布给"雇主" http://www.myvisajobs.com/Search_Visa_Sponsor.aspx?N=

中的表单

这是我到目前为止在Python中尝试的内容:

import requests
url = "http://www.myvisajobs.com/Search_Visa_Sponsor.aspx?N="
data = {"ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$txtCompany":"Microsoft"}
r = requests.post(url,data)
print(r.text)

仅返回原始HTML。我试图返回生成的HTML。我的直觉是我做了一些根本错误的事情,但我不确定是什么。

1 个答案:

答案 0 :(得分:1)

搜索POST请求中发送的参数多于参考公司名称的ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$txtCompany

相反,为了使事情变得透明和简单,我会使用RoboBrowser来自动填充"其他形式的POST参数需要。工作代码示例:

from robobrowser import RoboBrowser


url = "http://www.myvisajobs.com/Search_Visa_Sponsor.aspx?N="

browser = RoboBrowser(history=True)
browser.open(url)

form = browser.get_form(id='aspnetForm')
form['ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$txtCompany'].value = 'Microsoft'
browser.submit_form(form)

results = browser.select('div#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_divContent table tr')[1:]
for result in results:
    cells = result.find_all("td")

    print(cells[2].get_text(strip=True))

它会从搜索结果中打印公司名称:

Microsoft Corporation
Microsoft Operations Puerto Rico, Llc
Microsoft Caribbean, Inc.
Standard Microsystems Corporation
4Microsoft Corporation
Microsoft Business Solutions Corporation
Microsoft C98052orporation
Microsoft Ccrporation
Microsoft Coiporation
Microsoft Copporation
Microsoft Corforation
Microsoft Licensing, GP
Microsoft Way
Microsoftech Inc
Quantitative Micro Software Llc
Webtv Networks Microsoft Sub
Microsoft
FAST, A Microsoft Subsidiary
Microsoft Corporation - Sham
Microsoft Partner Careers (sponsored By Microsoft Dynamics)
Microsoft Iberica
Microsoft Karthi