文章目录
- requests模拟登录
- QWebEngine登录
- 存储数据
- PySide2使用QWebEngineView报错extension_system_qt.cpp(122) failed to parse extension manifest
requests模拟登录
- 使用requests发送post请求;
- 获取响应头中的Set-Cookie的值;
- 将该Cookie值存入浏览器对应的域名,测试是否可用。
可以使用
import requests
url = "http://localhost:5050/login/"
def login(url, headers, data):
res = requests.post(url, headers=headers, data=data)
print("response code:", res.status_code)
print("content:", res.headers)
if __name__ == "__main__":
"""
Host: localhost:5050
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept: */*
Connection: keep-alive
Cookie: session=.eJwlzjESAjEIAMC_UFsAISTcZ25CCKPtnVc5_l0d6232BXse67zD9jyudYP9EbCBG3qMoDGVSZYP0mjGJZ0Kkk0tTYh6IrNzuqvFIEK1ZKKhOdVqGV1EqIi1iVHcrGFG7yqNf0S8ljSUrDpYxdKnYmKN7gbfyHWu478heH8Ad3suDA.ZEU0hw.5N9yfwPep8tDGT-Y9wxfLXR3Ohw
"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
}
data = {
"username": "admin",
"password": "admin"
}
login(url, headers, data=data)
QWebEngine登录
将以上获取的Cookie信息,设置QWebEngine的cookie,实现模拟登录。
# __author__ = "laufing"
# class_based_qt
# laufing_qt
import sys
import time
import sys
from urllib.parse import urlparse
from PySide2.QtWidgets import *
from PySide2.QtCore import * # QByteArray
from PySide2.QtGui import *
from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile
from PySide2.QtWebEngineCore import QWebEngineHttpRequest, QWebEngineCookieStore
from PySide2.QtNetwork import QNetworkCookie
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
# 窗口标题
self.setWindowTitle("laufing[*]")
# 窗口图标
pixmap = QPixmap("./imgs/dog.jpg").scaled(50, 50)
icon = QIcon(pixmap)
# icon.addPixmap(pixmap, QIcon.Normal, QIcon.Off)
self.setWindowIcon(icon)
# 设置宽高
self.resize(900, 800)
# 窗口居中
desktop_geo = QDesktopWidget().geometry()
width, height = desktop_geo.width(), desktop_geo.height()
self.move(width / 2 - self.width() / 2, height / 2 - self.height() / 2)
# 设置文本
self.label = QLabel(self)
self.label.setText("测试按钮被按下")
self.label.resize(200, 200)
self.label.move(100, 100)
self.setUI()
def setUI(self):
# 实例化浏览器引擎视图
self.web_view = QWebEngineView(self)
self.web_view.setFixedWidth(900)
self.web_view.setFixedHeight(800)
# 创建http请求
self.req = QWebEngineHttpRequest()
# 设置请求的url
self.url = url = "http://localhost:5050/chart/add"
# 解析url
parsed_url = urlparse(url)
self.domain = parsed_url.scheme + "://" + parsed_url.netloc
# ParseResult(scheme='http', netloc='localhost:5050', path='', params='', query='', fragment='')
# parsed_url.scheme 获取协议
# parsed_url.netloc 获取域名
self.req.setUrl(QUrl(url)) # 请求对象设置url
# 设置cookie
self.set_cookie()
# 浏览器引擎 加载请求对象 or QUrl
self.web_view.page().load(self.req)
def set_cookie(self, cookie=None):
# cookie
cookie = "session=.eJwlzjEOwjAMQNG7ZGawHdeJuQyKY1sgVSC1MCHuThHj01_-u1xyi_1azs_tFadyuXk5F1MwH45jCiGHDRRvSjUNK6BOqY0RewKRUZqJ-kAE0STEITlFlzo6M2NlbRO8mmqD9N6FG_0SUgQ34FxkkLCmTYGExbtpOUZee2z_Gzy4PuZY40Dcy-cLGa8yPQ.ZEU0jA.XhyzfsXhTxQC0j0UmphlC8nRe5Q"
# 解析cookie 并导入
cookie_dict = self.parse_cookie(cookie)
for key, value in cookie_dict.items():
# 生成单个cookie
key_bytes = QByteArray(key.encode())
val_bytes = QByteArray(value.encode())
cookie_str = QNetworkCookie(key_bytes, val_bytes)
print("cookie:", cookie_str)
# 设置cookie 及 对应的网站域名
self.web_view.page().profile().cookieStore().setCookie(cookie_str, self.domain)
# 设置cookie持久化
self.web_view.page().profile().setPersistentCookiesPolicy(
QWebEngineProfile.PersistentCookiesPolicy.AllowPersistentCookies)
def parse_cookie(self, cookie_str):
"""解析cookie"""
c_list = cookie_str.split(";")
c_dict = dict()
for i in c_list:
key, val = i.split('=')
c_dict[key] = val
return c_dict
"""
# 也可以如下设置:
my_cookie = QNetworkCookie()
my_cookie.setName(key.encode())
my_cookie.setValue(values.encode())
my_cookie.setDomain('xx.xx.com')
xxx.setCookie(c, domain)
"""
if __name__ == '__main__':
# 窗口应用程序
app = QApplication(sys.argv)
window = Window()
window.show()
status_code = app.exec_()
sys.exit(status_code)
存储数据
- 程序自己存入db
- 爬虫请求api 添加数据集(表名信息)
# GET
url2 = "http://localhost:5050/explore/"
headers = {
"Host": "localhost:5050",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
"Accept-Encoding": "gzip, deflate, br",
"Accept": "*/*",
"Connection": "keep-alive",
"Cookie": "session=.eJwlzjEOwjAMQNG7ZGawHdeJuQyKY1sgVSC1MCHuThHj01_-u1xyi_1azs_tFadyuXk5F1MwH45jCiGHDRRvSjUNK6BOqY0RewKRUZqJ-kAE0STEITlFlzo6M2NlbRO8mmqD9N6FG_0SUgQ34FxkkLCmTYGExbtpOUZee2z_Gzy4PuZY40Dcy-cLGa8yPQ.ZEU0jA.XhyzfsXhTxQC0j0UmphlC8nRe5Q"
}
params = {
"form_data_key": "jiB0jEm4eDbdMs0cbkkT6d4aPUOm5NYKShbl0664CYXkuiNYPOutF626jTwTkucO",
"datasource_type": "table",
"datasource_id": 3
}
def get_dataset(url, headers, params):
res = requests.get(url, headers=headers, params=params)
print("响应状态:", res.status_code)
print("响应的数据:", res.text)
# 删除
# http://localhost:5050/api/v1/dataset/
# ?q=(order_column:changed_on_delta_humanized,order_direction:desc,page:0,page_size:25)
# 添加dataset
add_url = "http://localhost:5050/api/v1/dataset/"
# post
payload = {
"database": 1, # 自身数据库
"schema": "superset",
"table_name": "ttt" # 表名
}
def add_dataset(url, headers, data):
res = requests.post(url, headers=headers, json=data) # 服务端要求json格式
print("add response:", res.status_code)
if __name__ == "__main__":
"""
请求头
Host: localhost:5050
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept: */*
Connection: keep-alive
Cookie: session=.eJwlzjESAjEIAMC_UFsAISTcZ25CCKPtnVc5_l0d6232BXse67zD9jyudYP9EbCBG3qMoDGVSZYP0mjGJZ0Kkk0tTYh6IrNzuqvFIEK1ZKKhOdVqGV1EqIi1iVHcrGFG7yqNf0S8ljSUrDpYxdKnYmKN7gbfyHWu478heH8Ad3suDA.ZEU0hw.5N9yfwPep8tDGT-Y9wxfLXR3Ohw
"""
# 添加数据到tables中(自己添加不行,uuid无法给值)
add_dataset(add_url, headers=headers, data=payload)
PySide2使用QWebEngineView报错extension_system_qt.cpp(122) failed to parse extension manifest
删除anaconda目录下的qt.conf