【问题标题】:Cookies and render for an angular js app using Phantomjs and python使用 Phantomjs 和 python 的 Angular js 应用程序的 Cookie 和渲染
【发布时间】:2016-06-19 15:16:57
【问题描述】:

我有一个可以正常调用网页的对象,即使使用add_cookie。但是,我想渲染一个大量使用 angularjs 的网站。出于某种原因,我似乎无法设置正确的 cookie,这是我从实时会话中提取的。因此,我不确定其中有多少是我弄错的 cookie-foo,以及我缺少多少需要发生的 angular-js-foo。如果人们在这里有意见,那就太好了,但即使是 cmets 指出我去哪里摄取也会有帮助,因为我自己并没有走得太远。我已经清除了以下值。甚至一些关于如何调试的指针。我的目标是在页面上呈现divs。

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from bs4 import BeautifulSoup

class WebPage():

    def __init__(self):

        dcap = dict(DesiredCapabilities.PHANTOMJS)
        dcap["phantomjs.page.settings.userAgent"] = (
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
            "(KHTML, like Gecko) Chrome/15.0.87"
        )
        self.driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
        self.driver.set_window_size(1024, 768)

    def get_page(self):
        url = "http://proadvisor.intuit.com/search/index-standalone.html?v2#/search-result?location=60613&distance=500"
        self.driver.get(url)
        cookie = {'aam_aud': 'a_value', 'ttax': 0}
        self.driver.add_cookie(cookie)
        page = BeautifulSoup(self.driver.page_source, "lxml")
        page.find_all("div")


if __name__ == '__main__':
    wp = WebPage()
    wp.get_page()

现在得到:

selenium.common.exceptions.WebDriverException: Message: Error Message => 'Can only set Cookies for the current domain' 由 Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"104"," Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:34940","User-Agent":"Python-urllib/3.3"},"httpVersion":"1.1 ","method":"POST","post":"{\"cookie\": {\"aam_aud\": \"a_value\", \"ttax\": 0}, \"sessionId\": \"A-hash\"}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory": "/","path":"/cookie","re​​lative":"/cookie","port":"","host":"","password":"","user":"", "userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal ":"/session/a_hash/cookie"}

【问题讨论】:

    标签: angularjs python-3.x cookies selenium-webdriver phantomjs


    【解决方案1】:

    您必须将域添加到您的 cookie。

    试试这样的:

    cookie = {'aam_aud': 'a_value', 'ttax': 0, 'domain' : http://proadvisor.intuit.com}
    

    您可能需要对基本 URL 进行一些尝试,但我认为这应该可行。您必须告诉 cookie 您想在哪些网站上使用该 cookie。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 2016-11-21
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多