浏览器打开微信公众号h5页面,增加cookie绕过微信授权登录

版权声明:Dont trouble trouble until trouble troubles you. https://blog.csdn.net/JOJOY_tester/article/details/75063556
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
#插入cookie需要与cookie相同的页面的域名,下面这个链接不需要登录就能打开
driver.get(r"https://xxxxx81722.htm")
cookies = {"name":"userId","value":"270000127243445"}
cookies1 = {"name":"QLZB_SESSIONID","value":"4B6250526A505A5A2F3532506F6446537552357167377A547638614744424345464D38474D644B6F626A453D"}
cookies2 = {"name":"JSESSIONID","value":"887DEB8BCD5E513A5D4CF041D481E467"}
cookies3 = {"name":"QL_PG_WB","value":"0"}
driver.add_cookie(cookie_dict=cookies)
driver.add_cookie(cookie_dict=cookies1)
driver.add_cookie(cookie_dict=cookies2)
driver.add_cookie(cookie_dict=cookies3)
#插完后需要刷新,就是登录状态
driver.refresh()
sleep(1)
driver.find_element_by_xpath("html/body/div[2]/a[1]").click()
driver.find_element_by_xpath("html/body/div[2]/a[2]").click()
sleep(5)
driver.quit()

这个网站,那个cookie有限期为1年。时间足够长了吧!

from selenium import webdriver
from time import sleep

#指定手机设备
mobileEmulation = {"deviceName":"Nexus 5"}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation',mobileEmulation)
driver = webdriver.Chrome(executable_path='chromedriver.exe',chrome_options=options)
#插入cookie需要同一个域名,下面这个链接不需要登录就能打开
driver.get(r"https://xxxxxxx08181722.htm")
driver.set_window_size(400,800)
cookies = {"name":"userId","value":"270000127243445"}
cookies1 = {"name":"QLZB_SESSIONID","value":"4B6250526A505A5A2F3532506F6446537552357167377A547638614744424345464D38474D644B6F626A453D"}
cookies2 = {"name":"JSESSIONID","value":"887DEB8BCD5E513A5D4CF041D481E467"}
cookies3 = {"name":"QL_PG_WB","value":"0"}
driver.add_cookie(cookie_dict=cookies)
driver.add_cookie(cookie_dict=cookies1)
driver.add_cookie(cookie_dict=cookies2)
driver.add_cookie(cookie_dict=cookies3)
#插完后需要刷新,就是登录状态
driver.refresh()
sleep(1)
driver.find_element_by_xpath("html/body/div[2]/a[1]").click()
driver.find_element_by_xpath("html/body/div[2]/a[2]").click()
sleep(2)
driver.quit()

总结:

  1. 插入cookie需要插入到和cookie相关的网页,找出这个公众号中不需要微信授权登录就能打开的页面;
  2. 把cookie通过add_cookie()增加到这个页面,这个需要注意cookie的格式,这个字典形式的name\value;
  3. cookie增加到这个页面之后,需要刷新操作才生效,可以增加一个refresh()操作。


展开阅读全文

没有更多推荐了,返回首页