import urllib.requestimport sysimport http.cookiejarimport urllib.parsefrom bs4 import BeautifulSoupimport codecsimport re#登录页面url = "http://www.abc.com/login.asp"#登录Post数据postdata =urllib.parse.urlencode({ "username":"abc","password":"abc" }).encode('utf-8')#构造headerheader = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding":"utf-8", "Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", "Connection":"keep-alive", "Host":"www.abc.com", "Referer":"http://www.abc.com/login.asp", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0" }req = urllib.request.Request(url,postdata,header)#获得cookiecj = http.cookiejar.CookieJar()opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))r = opener.open(req)#继续访问登陆后的页面get_url = 'http://www.abc.com/user/' get_request = urllib.request.Request(get_url, headers=header)get_response = opener.open(get_request)#打印获得的页面信息print(get_response.read().decode("utf-8"))#用bs4可以获得你需要的标签内容soup = BeautifulSoup(get_response.read(),"html.parser")jie = soup.find_all('span')#这里利用正则表达式过滤掉html语言dr = re.compile(r'<[^>]+>',re.S)#这里jie[2]表示取符合条件的第三个标签dd = dr.sub('',str(jie[2]))print(dd)
以上这篇Python3 Post登录并且保存cookie登录其他页面的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持VEVB武林网。