使用selenium模拟登陆oschina
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的更多相关文章
- Selenium模拟登陆百度贴吧
Selenium模拟登陆百度贴吧 from selenium import webdriver from time import sleep from selenium.webdriver.commo ...
- selenium 模拟登陆豆瓣,爬取武林外传的短评
selenium 模拟登陆豆瓣,爬去武林外传的短评: 在最开始写爬虫的时候,抓取豆瓣评论,我们从F12里面是可以直接发现接口的,但是最近豆瓣更新,数据是JS异步加载的,所以没有找到合适的方法爬去,于是 ...
- 使用selenium模拟登陆新浪微博
1.selenium基本使用 1.selenium安装及基本操作 selenium是一个自动化测试工具,它支持各种浏览器,包括Chrome,Safari,Firefox等主流界面浏览器驱动,也包括Ph ...
- 验证码破解 | Selenium模拟登陆微博
模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...
- python selenium模拟登陆163邮箱。
selenium是可以模拟浏览器操作. 有些爬虫是异步加载的,通过爬取网页源码是得不到需要的内容.所以可以模拟浏览器去登陆该网站进行爬取操作. 需要安装selenium通过pip install xx ...
- 使用selenium模拟登陆淘宝、新浪和知乎
如果直接使用selenium访问淘宝.新浪和知乎这些网址.一般会识别出这是自动化测试工具,会有反制措施.当开启开发者模式后,就可以绕过他们的检测啦.(不行的,哭笑) 如果网站只是对windows.na ...
- Python爬虫 —— 知乎之selenium模拟登陆获取cookies+requests.Session()访问+session序列化
代码如下: # coding:utf-8 from selenium import webdriver import requests import sys import time from lxml ...
- python selenium模拟登陆qq空间
不多说.直接上代码 from selenium import webdriver driver = webdriver.Chrome() driver.get('http://qzone.qq.com ...
- 使用python - selenium模拟登陆b站
思路 输入用户名密码点击登陆 获取验证码的原始图片与有缺口的图片 找出两张图片的缺口起始处 拖动碎片 功能代码段 # 使用到的库 from selenium import webdriver from ...
随机推荐
- java缓存技术的介绍
一.什么是缓存1.Cache是高速缓冲存储器 一种特殊的存储器子系统,其中复制了频繁使用的数据以利于快速访问2.凡是位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之 ...
- Python 学习 第六篇:迭代和解析
Python中的迭代是指按照元素的顺序逐个调用的过程,迭代概念包括:迭代协议.可迭代对象和迭代器三个概念. 迭代协议是指有__next__()函数的对象会前进到下一个结果,而到达系列的末尾时,则会引发 ...
- .Net并行编程(一)-TPL之数据并行
前言 许多个人计算机和工作站都有多个CPU核心,可以同时执行多个线程.利用硬件的特性,使用并行化代码以在多个处理器之间分配工作. 应用场景 文件批量上传 并行上传单个文件.也可以把一个文件拆成几段分开 ...
- MRT与MRTS工具官宣退休,推荐使用HEG
今天错误的删除搞丢了之前下载的MRT与MRTS工具,浏览Modis官网下载时发现找不到了,后来在其官网上发现了这则通知,原来早已停止更新的MRT这次彻底退修了.通知原文如下~~~ The downlo ...
- Linux下批量ping某个网段ip的脚本
比如现在需要对172.16.50.0/24网段的ip进行检查,检查哪些ip现在被占用,哪些ip没有被占用,可以通过ping命令来检查,脚本如下: [root@uatdns01 opt]# vim /o ...
- C. Ehab and a 2-operation task
链接 [https://codeforces.com/contest/1088/problem/C] 题意 n个数,最多n+1操作,要么前i个数加x,要么前i个数对x取余,最后使得严格递增 分析 直接 ...
- B. Interesting drink
链接 [http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/B] 题意 给你n个数,q次查询,每次输入一个m,问n个数中有多少个数小于 ...
- TextView设置文字包含中英文时自动换行问题的终极解决方案
情景,正常TextView中设置文本内容中包含中英文时会造成自动换行的问题,影响界面显示效果,如图: 网上很多解决途径,甚至有多三方框架处理,但是效果并不能达到,最终是要如下代码完美解决,效果图如下: ...
- HTML 5 Canvas vs. SVG
pick up from http://www.w3school.com.cn/html5/html_5_canvas_vs_svg.asp Canvas 与 SVG 的比较 下表列出了 canvas ...
- Java解析Excel
前两天总结了些关于Excel和CSV结合TestNG进行数据驱动测试的例子,对于Excel存放TestCase和关键字如何进行解析,也做了对应的总结,希望在学习的路上勇往直前,有不对的地方,希望大家指 ...