使用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 ...
随机推荐
- C_数据结构_栈
# include <stdio.h> # include <malloc.h> # include <stdlib.h> typedef struct Node ...
- python-知识回顾-16
知识回顾 小数据池:int -5~256str 特殊字符,*数字20 ascii : 8位 1字节 表示1个字符unicode 32位 4个字节 表示一个字符utf- 8 1个英文 8位,1个字节 欧 ...
- 002-打开文件管理规范-20190406.bat
rem 002-打开文件管理规范-20190406.bat start /max https://www.cnblogs.com/delphixx/p/10652763.htmlcopy %~0 C: ...
- 个人作业Week1
一.<构建之法>提问 1.需求是什么?需求的规范需要明确吗? 2.一个人开发效率非常高,多人开发,个人效率随团队人数上升而直线下降,我们一般需要将大项目拆为小项目,使协作耦合产生的效率负影 ...
- [BUAA软工]第一次博客作业---阅读《构建之法》
[BUAA软工]第一次博客作业 项目 内容 这个作业属于哪个课程 北航软工 这个作业的要求在哪里 第1次个人作业 我在这个课程的目标是 学习如何以团队的形式开发软件,提升个人软件开发能力 这个作业在哪 ...
- Eclipse+tomcat+MySQL搭建JavaWeb开发环境
准备 1.安装JDK并配置环境变量 2.下载Eclipse,Tomcat,MySQL 配置eclipse 解压Eclipse,需要添加Web Tools Platform才能进行web开发 1.启动E ...
- Practice4 阅读《构建之法》6-7章
关于第五章后面的阅读已经在Practice3中有所感悟,下面是6-7章的读书笔记. 第6章 敏捷流程这一章讲了“敏捷流程”这一概念,关于这一名词我是很陌生的,在阅读之后有了一定的理解.敏捷流程是提供了 ...
- Chrome查看HTTP
查找cookie 补充: 接口调试使用postman挺不错的.以前每次都自己写一个ajax来进行接收调试. 如:用post发送json数据给接口,得到json数据. 工具有时候能让效率大大提升,要学会 ...
- [2017BUAA软件工程]第0次作业
第一部分:结缘计算机 1. 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢?(必答) 选择计算机专业的一个重要原因是因为计算机专业的就业前景好,由于计算机本身具有的各种优点,现在几乎所有的 ...
- java List<String>的初始化
今天在处理生成excel的时候用到了java的list,但是需要直接赋值固定的几个变量,如果先初始化然后add的方法: List<String> name = new ArrayList( ...