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. item 8: 比起0和NULL更偏爱nullptr

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 先让我们看一些概念:字面上的0是一个int,不是一个指针.如果C+ ...

  2. layui表格和弹出框的简单示例

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  3. Sql_索引分析

    「索引就像书的目录, 通过书的目录就准确的定位到了书籍具体的内容」,这句话描述的非常正确, 但就像脱了裤子放屁,说了跟没说一样,通过目录查找书的内容自然是要比一页一页的翻书找来的快,同样使用的索引的人 ...

  4. Bash : 冒泡排序

    冒泡排序是非常基础的排序算法,本文我们看看在 Bash 脚本中如何写冒泡排序.本文的演示环境为 ubuntu 16.04. 冒泡排序的简要描述如下: 通过连续的比较对数组中的元素进行排序 比较两个相邻 ...

  5. openssl版本升级操作记录

    需要部署nginx的https环境,之前是yum安装的openssl,版本比较低,如下: [root@nginx ~]# yum install -y pcre pcre-devel openssl ...

  6. MySQL高可用方案-PXC环境部署记录

    之前梳理了Mysql+Keepalived双主热备高可用操作记录,对于mysql高可用方案,经常用到的的主要有下面三种: 一.基于主从复制的高可用方案:双节点主从 + keepalived 一般来说, ...

  7. 先埋锅-CF-Valid BFS?-差一点没交上

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  8. linux第一次读书笔记

    第一章 LINUX内核简介 1.1 Unix的历史 1969年的夏天,贝尔实验室的程序员们在一台PDR-7型机上实现了Unix这个全新的操作系统. 1973年,整个Unix系统用C语言进行了重写,给后 ...

  9. 自己实现数据结构系列二---LinkedList

    一.先上代码: 1.方式一: public class LinkedList<E> { //节点,用来存放数据:数据+下一个元素的引用 private class Node{ privat ...

  10. MySQL: Connection Refused,调整 mysql.ini中的 max_connections

    连接相同的结构的MySQL数据库,一套库Tomcat启动正常,另一套库一直报Connection Refused. 可以断定是连接数太小了.查找mysql.ini中的 max_connections, ...