Selenium把元素定位接口封装得更简单易用了,支持Xpath、CSS选择器、以及标签名、标签属性和标签文本查找。

from selenium.webdriver import PhantomJS
from random import randint
import time
from selenium.webdriver.common.keys import Keys
from requests.cookies import RequestsCookieJar
import requests def savepic():
filename = '{}-{}.png'.format(int(time.time()), randint(100, 999))
driver.save_screenshot(filename=filename) with PhantomJS() as driver:
driver.set_window_size(width=1280, height=1024)
url = 'https://www.oschina.net/home/login'
driver.get(url=url)
# savepic() username = driver.find_element_by_id(id_='userMail')
password = driver.find_element_by_id(id_='userPassword')
username.send_keys('user@xx.com') # 输入用户名
password.send_keys('password') # 输入密码
# savepic() password.send_keys(Keys.ENTER) # 输入回车,提交表单
time.sleep(10)
print(driver.current_url) # 登陆后跳转到首页
# userinfo = driver.find_element_by_class_name(name='user-info')
while not driver.find_element_by_class_name(name='user-info').is_displayed():
time.sleep(1)
savepic() cookies = driver.get_cookies() # 获取cookie
print(cookies, type(cookies))
for cookie in cookies:
print(cookie) jar = RequestsCookieJar()
for cookie in cookies:
jar.set(name=cookie.get('name'), value=cookie.get('value'))
print(jar) ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
headers = {'user-agent': ua} with requests.get(url=url, headers=headers) as resp:
print(resp.url) # 不带cookie会停留在登陆页 with requests.get(url=url, headers=headers, cookies=jar) as resp:
print(resp.url) # 带上cookie会自动登陆跳转到首页
with open('osc.html', 'wb') as f:
f.write(resp.content)

参考:

https://selenium-python.readthedocs.io/locating-elements.html

https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys

使用selenium模拟登陆oschina的更多相关文章

  1. Selenium模拟登陆百度贴吧

    Selenium模拟登陆百度贴吧 from selenium import webdriver from time import sleep from selenium.webdriver.commo ...

  2. selenium 模拟登陆豆瓣,爬取武林外传的短评

    selenium 模拟登陆豆瓣,爬去武林外传的短评: 在最开始写爬虫的时候,抓取豆瓣评论,我们从F12里面是可以直接发现接口的,但是最近豆瓣更新,数据是JS异步加载的,所以没有找到合适的方法爬去,于是 ...

  3. 使用selenium模拟登陆新浪微博

    1.selenium基本使用 1.selenium安装及基本操作 selenium是一个自动化测试工具,它支持各种浏览器,包括Chrome,Safari,Firefox等主流界面浏览器驱动,也包括Ph ...

  4. 验证码破解 | Selenium模拟登陆微博

    模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...

  5. python selenium模拟登陆163邮箱。

    selenium是可以模拟浏览器操作. 有些爬虫是异步加载的,通过爬取网页源码是得不到需要的内容.所以可以模拟浏览器去登陆该网站进行爬取操作. 需要安装selenium通过pip install xx ...

  6. 使用selenium模拟登陆淘宝、新浪和知乎

    如果直接使用selenium访问淘宝.新浪和知乎这些网址.一般会识别出这是自动化测试工具,会有反制措施.当开启开发者模式后,就可以绕过他们的检测啦.(不行的,哭笑) 如果网站只是对windows.na ...

  7. Python爬虫 —— 知乎之selenium模拟登陆获取cookies+requests.Session()访问+session序列化

    代码如下: # coding:utf-8 from selenium import webdriver import requests import sys import time from lxml ...

  8. python selenium模拟登陆qq空间

    不多说.直接上代码 from selenium import webdriver driver = webdriver.Chrome() driver.get('http://qzone.qq.com ...

  9. 使用python - selenium模拟登陆b站

    思路 输入用户名密码点击登陆 获取验证码的原始图片与有缺口的图片 找出两张图片的缺口起始处 拖动碎片 功能代码段 # 使用到的库 from selenium import webdriver from ...

随机推荐

  1. POJ Remmarguts' Date

    题目链接-> 题解: 次短路模板. 代码: #include<cstdio> #include<iostream> using namespace std; #defin ...

  2. 七年一冠、IG牛13的背后是什么!

    最近忙着看S8世界总决赛,博客荒废了近一个月,后续步入正轨.   2018年11月3日.S8世界总决赛.中国终于夺得了S系列赛的总冠军. “IG牛逼”也开始刷爆社交圈,对于在S3入坑的我来说,也弥补上 ...

  3. Vue 实际项目中你可能会遇见问题

    纸上得来终觉浅,绝知此事要躬行! Vue的文档和教程看的太多,小的demo做的多,也不如自己实际的进行一个完整项目的开发.只有做了才知道原来问题这么多,这里列举了一些你做demo教程可能不会遇见的坑. ...

  4. MVC使用Redis实现分布式锁

    使用场景 在做Web项目的时候,有很多特殊的场景要使用到锁 比如说抢红包,资源分配,订单支付等场景 就拿抢红包来说,如果一个红包有5份,同时100个人抢如果没有用到锁的话 100个人同时并发都抢成功, ...

  5. RabbitMQ TroubleShooting

    RabbitMQ是一款优秀的消息队列中间件,提供了稳定.监控完善的产品,但是软件就会有bug.为了前进路径可以畅通,我们必须了解出现的一些故障的快速处理方式,毕竟在生产环境,时间就是生命,尽快的处理是 ...

  6. Centos7.2下OpenVPN 环境完整部署记录

    关于OpenVPN的有关介绍及为何使用OpenVPN在此就不做赘述了,下面直接记录Centos7.2系统下部署OpenVPN环境的操作过程: 1) 先将本机的yum换成阿里云的yum源 [root@t ...

  7. 在Mac终端显示 Git 当前所在分支

    1.进入你的home目录 cd ~ 2.编辑.bashrc文件 vi .bashrc 3.将下面的代码加入到文件的最后处 function git_branch { branch="`git ...

  8. C_数据结构_数组

    //数组 # include <stdio.h> # include <malloc.h> //包含了 malloc 函数 # include <stdlib.h> ...

  9. linux第三次实践:ELF文件格式分析

    linux第三次实践:ELF文件格式分析 标签(空格分隔): 20135328陈都 一.概述 1.ELF全称Executable and Linkable Format,可执行连接格式,ELF格式的文 ...

  10. spatial-temporal information extraction典型方法总结

    ==================================== 咳咳咳 由于科研的直接对象就是video sequence,所以,如何更好地提取spatial-temporal inform ...