Python3 Post登录并且保存cookie登录其他页面的方法
作者:队长小楠 时间:2023-08-18 22:45:52
如下所示:
import urllib.request
import sys
import http.cookiejar
import urllib.parse
from bs4 import BeautifulSoup
import codecs
import re
#登录页面
url = "http://www.abc.com/login.asp"
#登录Post数据
postdata =urllib.parse.urlencode({
"username":"abc",
"password":"abc"
}).encode('utf-8')
#构造header
header = {
"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)
#获得cookie
cj = 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)
来源:https://blog.csdn.net/u013630017/article/details/52434470
标签:Python3,Post,cookie
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一文详解Go语言切片是如何扩容的
2024-04-26 17:29:35
![](https://img.aspxhome.com/file/2023/0/134030_0s.png)
win7 x64系统中安装Scrapy的方法
2023-10-19 04:04:36
![](https://img.aspxhome.com/file/2023/3/95553_0s.jpg)
python中ImageTk.PhotoImage()不显示图片却不报错问题解决
2023-08-26 18:12:06
![](https://img.aspxhome.com/file/2023/9/69589_0s.png)
centos7利用yum安装mysql 8.0.12
2024-01-26 08:09:45
![](https://img.aspxhome.com/file/2023/4/115334_0s.png)
如何使用python-opencv批量生成带噪点噪线的数字验证码
2023-10-14 03:38:54
![](https://img.aspxhome.com/file/2023/2/92212_0s.png)
Python将xml和xsl转换为html的方法
2022-04-02 14:24:32
Go interface{} 转切片类型的实现方法
2024-05-05 09:31:05
sql语言中delete删除命令语句详解
2024-01-28 02:20:02
浅析Mysql和Oracle分页的区别
2024-01-23 16:38:49
![](https://img.aspxhome.com/file/2023/6/67026_0s.png)
python切片的步进、添加、连接简单操作示例
2022-09-10 01:26:34
Python微服务开发之使用FastAPI构建高效API
2022-04-20 21:51:13
![](https://img.aspxhome.com/file/2023/2/134902_0s.webp)
django中操作mysql数据库的方法
2024-01-18 13:37:55
![](https://img.aspxhome.com/file/2023/9/133709_0s.png)
Win10下python 2.7.13 安装配置方法图文教程
2023-01-13 20:12:27
![](https://img.aspxhome.com/file/2023/4/80624_0s.png)
java连接Oracle数据库的方法解析
2024-01-21 22:12:27
![](https://img.aspxhome.com/file/2023/4/65584_0s.png)
Windows10下mysql 8.0.19 安装配置方法图文教程
2024-01-21 06:33:27
![](https://img.aspxhome.com/file/2023/7/107407_0s.jpg)
利用Python编写简易的录制屏幕小工具
2023-12-17 04:34:57
![](https://img.aspxhome.com/file/2023/0/93690_0s.gif)
Python实现名片管理系统
2022-01-11 21:45:12
![](https://img.aspxhome.com/file/2023/9/101469_0s.jpg)
python实现京东订单推送到测试环境,提供便利操作示例
2021-09-01 09:03:46
Python编程应用设计原则详解
2021-04-08 20:13:05
![](https://img.aspxhome.com/file/2023/6/107076_0s.jpg)
解决Pandas to_json()中文乱码,转化为json数组的问题
2023-12-05 19:06:33
![](https://img.aspxhome.com/file/2023/1/115001_0s.jpg)