selenium登录网站获取cookies请求其他数据
2021-05-15 19:29
sites = []
s = requests.Session()
start_url = ‘https://passport.umeng.com/login?appId=cnzz‘
try:
print ‘登录友盟‘
driver.delete_all_cookies()
driver.get(start_url)
# print driver.current_url ,driver.page_source
driver.switch_to.frame("alibaba-login-box") #进入登录iframe
time.sleep(10)
elem_user = driver.find_element_by_id("fm-login-id")
time.sleep(10)
elem_user.send_keys("pjq518@qq.com");
elem_pwd = driver.find_element_by_id(‘fm-login-password‘)
time.sleep(10)
elem_pwd.send_keys("tangdaoya2016")
time.sleep(10)
elem_pwd.send_keys(Keys.ENTER) #网站模拟点击登录按钮会出错,直接在密码输完后enter进行登录即可
time.sleep(10)
if driver.current_url != start_url:
print ‘登陆成功‘,driver.current_url
driver.switch_to_default_content()#需要返回主页面,不然获取的cookies不是登陆后cookies
list_cookies = driver.get_cookies()
cookies = {}
for s in list_cookies:
cookies[s[‘name‘]] = s[‘value‘]
requests.utils.add_dict_to_cookiejar(s.cookies, cookies) #将获取的cookies带入请求中
print ‘cookies:‘,cookies,self.config.url_list
url_list = ‘ttps://web.umeng.com/main.php?c=site&a=show&ajax=module=list|module=isOpenTongji&search=¤tPage=1&pageType=30&sort=0‘ #url_list是登陆后才能请求到数据的api
r = json.loads(s.get(url_list).content)
for temp in r[‘data‘][‘list‘][‘items‘]:
if isinstance(temp,dict):
sites.append({"siteid":temp["siteid"],"name":temp["name"]})
except:
print ‘登陆失败‘
traceback.print_exc()
finally:
driver.quit()
display.stop()
文章标题:selenium登录网站获取cookies请求其他数据
文章链接:http://soscw.com/index.php/essay/85922.html