关于python对于aspx网页的post提交

这二天看了一下python,比较这种编程思想和开源。试了一下自己的ASPX想实现一个自动登录!

结果查询了许久没有结果,老是返回到登录页面,后请教单位的大牛讲了一下,原来还是有很多数据要提交。

晚上继续G,终于找到了解决方案。特记录下来,以后备后查!

解决方案一、
import requests
from bs4 import BeautifulSoup

URL="http://www11.davidsonsinc.com/Login/Login.aspx"
headers={
          
   "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"}

username="username"
password="password"

s=requests.Session()
s.headers.update(headers)
r=s.get(URL)
soup=BeautifulSoup(r.content)

VIEWSTATE=soup.find(id="__VIEWSTATE")[value]
VIEWSTATEGENERATOR=soup.find(id="__VIEWSTATEGENERATOR")[value]
EVENTVALIDATION=soup.find(id="__EVENTVALIDATION")[value]

login_data={
          
   "__VIEWSTATE":VIEWSTATE,
"__VIEWSTATEGENERATOR":VIEWSTATEGENERATOR,
"__EVENTVALIDATION":EVENTVALIDATION,
"ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$UserName":username,
"ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$Password":password,
"ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$LoginButton":"Log In"}

r=s.post(URL, data=login_data)
print r.url
解决方案二 I was initially using requests+bs4 as well however I was running into similar issues with the ASPX site Im scrapping. I found another library called that wraps requests+bs4. With this you no longer have to manually set items such as "__VIEWSTATE" and friends when interacting with ASPX sites.

from robobrowser import RoboBrowser

url =  http://www11.davidsonsinc.com
login_url = url + /Login/Login.aspx

username = "username"
password = "password"

browser = RoboBrowser(history=True)
# This retrieves __VIEWSTATE and friends
browser.open(login_url)

signin = browser.get_form(id=aspnetForm)
signin["ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$UserName"].value = username
signin["ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$Password"].value = password
signin["ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$LoginButton"].value = "Log In"
browser.submit_form(signin)
print browser.url
(第一次写博客,不周之处请大家见谅!)
0 I was initially using requests+bs4 as well however I was running into similar issues with the ASPX site Im scrapping. I found another library called that wraps requests+bs4. With this you no longer have to manually set items such as "__VIEWSTATE" and friends when interacting with ASPX sites.
0 I was initially using requests+bs4 as well however I was running into similar issues with the ASPX site Im scrapping. I found another library called that wraps requests+bs4. With this you no longer have to manually set items such as "__VIEWSTATE" and friends when interacting with ASPX sites.
经验分享 程序员 微信小程序 职场和发展