【问题标题】:Python 3 | PyQt5 - QWebEngineView + urllib + beautifulsoup with Cookies蟒蛇 3 | PyQt5 - QWebEngineView + urllib + beautifulsoup with Cookies
【发布时间】:2017-08-01 18:31:00
【问题描述】:

我正在尝试使用 PyQt5 制作一个带有 GUI 的应用程序,它在 Instagram 上做它的事情。这是我坚持的一个功能:

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,他们的帐户设置为仅向朋友显示他们的图片,我在 Python 中收到“404 not found”错误。 所以我必须以某种方式登录,才能访问这些类型的图像。

所以我的问题是如何从我的 Chrome 浏览器导入我的 Cookie,并将它们与请求一起发送,以便我登录,并且当然保持登录状态 - 这样我就可以在保持登录状态的同时使用 QWebEngineView 浏览页面并在没有失败的情况下执行 urllib.requests。

环境: Python 3.6.2、PyQt5.9、Windows10x64

【问题讨论】:

    标签: python-3.x cookies beautifulsoup urllib pyqt5


    【解决方案1】:

    我不确定是否要从 Chrome 导入您的 cookie,但您可以使用以下代码将它们持久保存在您的应用中。您必须登录一次才能设置 cookie,但下次运行您的应用时,您不需要再次登录。

    defaultProfile = QWebEngineProfile.defaultProfile()
    defaultProfile.setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
    cookies_path = '/your_app_cookie_path_here'
    defaultProfile.setCachePath(cookies_path)
    defaultProfile.setPersistentStoragePath(cookies_path)
    

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多