Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python获取此url(;来自微信);?_Python_Selenium_Cookies_Wechat - Fatal编程技术网

如何使用python获取此url(;来自微信);?

如何使用python获取此url(;来自微信);?,python,selenium,cookies,wechat,Python,Selenium,Cookies,Wechat,以下是我的Python代码: import time from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=chro

以下是我的Python代码:

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    driver = webdriver.Chrome(chrome_options=chrome_options)
    
    testUrl ='http://mp.weixin.qq.com/s?__biz=MzA4ODIxNjY1OA==&mid=2650747988&idx=1&sn=de85a17895c5e28eb9e4142cdeccbdaf&chksm=88261169bf51987f66f41f5a2dc850af2e30ae77f01c82c45eb4ecd619714daa970b6120c834&scene=27#wechat_redirect'
    
    
    geturl = driver.get(testUrl)
    time.sleep(4) 
    
    html = driver.execute_script("return document.documentElement.outerHTML")
    with open('selenium.txt', 'w', encoding='utf-8') as f:
    
    f.write(html)  
    driver.close()


这个URL可以在chrome或safari中查看,没有任何问题,但是我通过上面的代码获得的内容是不同的。它要求我提供cookie,以便可以加载页面。我该怎么做呢?

有两种方法:

使用与普通浏览器一起使用的userdata文件夹

另一种连接方式是连接到已登录的浏览器会话:

请遵循以下步骤:

  • 以以下方式打开chrome.exe:
  • 从selenium连接到现有chrome浏览器: 关闭所有chrome浏览器,然后在调试模式下启动chrome浏览器,方法是打开cmd并使用-remote debug port参数运行chrome.exe

    "<path>\chrome.exe" --remote-debugging-port=1557
    

    方法1不起作用,我得到的页面静止报告需要cookie才能继续加载真实的文本内容。@而且您已经登录到该浏览器了吗?方法1和方法2只有在您可以手动打开浏览器的情况下才起作用,但由于使用了新的chrome配置文件,自动操作失败。方法2也不起作用,它报告“无法连接到127.0.0.1:9222的chrome”您是否使用“\chrome.exe”--远程调试端口=9222打开chrome
    "<path>\chrome.exe" --remote-debugging-port=1557
    
    ChromeOptions options = new ChromeOptions();
    options.add_experimental_option("debuggerAddress", "127.0.0.1:1557");
    browser=new ChromeDriver(options);