Python 3 | PyQt5 - QWebEngineView + urllib + beautifulsoup与Cookies
问题描述:
我试图用GUI来制作一个应用程序,使用PyQt5在Instagram上做它的事情。下面是我被困在一个函数:Python 3 | PyQt5 - QWebEngineView + urllib + beautifulsoup与Cookies
def pro_smt(self):
url = "https://www.instagram.com/someonespicture123"
sauce = urllib.request.urlopen(url)
soup = bs.BeautifulSoup(sauce, 'lxml')
#I use this to check whether the profile is public, and therefore accessible without loggin in.
self.browserWindow.load(QUrl(url))
self.browserWindow.show()
问题是,如果我输入某人的URL,谁都有自己的帐户设置为只显示自己的图片唯一的朋友,我得到了一个“404未找到”错误蟒蛇。 所以我必须以某种方式登录,以便我可以访问这些图像。
所以我的问题是如何从我的Chrome浏览器中导入我的Cookie,并向他们发送请求,以便我登录并保持登录状态 - 以便我可以在停留时使用QWebEngineView浏览页面登录,并做urllib.requests没有他们失败。
环境: 的Python 3.6.2,PyQt5.9,Windows10x64
答
我不知道从Chrome中导入您的饼干,但你可以用下面的代码您的应用程序坚持他们。您必须登录一次才能设置您的Cookie,但下一次运行应用程序时,您不需要再次设置。
defaultProfile = QWebEngineProfile.defaultProfile()
defaultProfile.setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
cookies_path = '/your_app_cookie_path_here'
defaultProfile.setCachePath(cookies_path)
defaultProfile.setPersistentStoragePath(cookies_path)