import requests
from selenium import  webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
chorme_option=Options()
chorme_option.add_argument("--disable-gpu")
chorme_option.add_argument("--disable-infobars")

#禁止图片加载
prefs = {
    "profile.default_content_setting_values" : {
        "images": 2
    },"profile.default_content_setting_values.notifications"  :  2
}
chorme_option.add_experimental_option("prefs",prefs)
chorme_option.add_argument('--ignore-certificate-errors') #SSLエラー対策
driver = webdriver.Chrome(chrome_options = chorme_option)
wait=WebDriverWait(driver,10)
print(u"开始登陆")
driver.get("https://www.weibo.com/login.php")

try:
    login_id=wait.until(
        EC.presence_of_element_located((By.XPATH,"//div[@class='login_innerwrap']//input[@id='loginname']"))
    )
    login_id.send_keys("username")
    login_id.send_keys(Keys.ENTER)
    password=wait.until(
        EC.presence_of_element_located((By.XPATH,"//div[@class='login_innerwrap']//input[@type='password']"))
    )
    password.send_keys("password")
    submit=driver.find_element_by_xpath("//a/span[@node-type='submitStates']")
    submit.click()
    req = requests.Session()  # 构建Session
    cookies = driver.get_cookies()  # 导出cookie
    print(cookies)
    driver.get("https://weibo.com/xxxx/profile?topnav=1&wvr=6&is_all=1")
    for cookie in cookies:
        req.cookies.set(cookie['name'], cookie['value'])  # 转换cookies
    test = req.get('https://weibo.com/xxxx/profile?topnav=1&wvr=6&is_all=1')
    print(test.text)
except:
    driver.close()

  

selenium+requests访问微博的更多相关文章

  1. Python——通过用户cookies访问微博首页

    通过用户cookies访问微博首页 1.登录微博 self.driver.delete_all_cookies() # 删除cookies self.driver.get(self.url) time ...

  2. [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能.        [Pyth ...

  3. 解决python2.7.9以下版本requests访问https的问题

    在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...

  4. python+selenium+requests爬取我的博客粉丝的名称

    爬取目标 1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块 selenium 2.53.6 +firefox 44 BeautifulSoup re ...

  5. python+selenium+requests爬取qq空间相册时遇到的问题及解决思路

    最近研究了下用python爬取qq空间相册的问题,遇到的问题及解决思路如下: 1.qq空间相册的访问需要qq登录并且需是好友,requests模块模拟qq登录略显麻烦,所以采用selenium的dri ...

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

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

  7. selenium+requests进行cookies保存读取操作

    看这篇文章之前大家可以先看下我的上一篇文章:cookies详解 本篇我们就针对上一篇来说一下cookies的基本应用 使用selenium模拟登陆百度 from selenium import web ...

  8. 用python+selenium抓取微博24小时热门话题的前15个并保存到txt中

    抓取微博24小时热门话题的前15个,抓取的内容请保存至txt文件中,需要抓取排行.话题和阅读数 #coding=utf-8 from selenium import webdriver import ...

  9. Python+Selenium学习--访问连接

    场景 web UI测试里最简单也是最基本的事情就是访问1个链接了. 在python的webdrive中,访问url时应该使用get方法. 代码 #!/usr/bin/env python # -*- ...

随机推荐

  1. 如何在vue中使用sass

    使用sass,我们需要安装sass的依赖包 npm install --save-dev sass-loader //sass-loader依赖于node-sass npm install --sav ...

  2. TypeScript笔记 4--变量声明

    在上一篇:基础变量中我们在声明变量时使用了关键字let,这和JS中的var有点类似. 语法 基本语法:let 变量名:类型.当然类型不是必须的. let x:number; let y:string ...

  3. ES6中promise的使用方法

    先看看ES5中异步编程的使用. let ajax = function (callBlack) { setTimeout(function () { callBlack && call ...

  4. b2b2c

    编辑 B2B2C是一种电子商务类型的网络购物商业模式,B是BUSINESS的简称,C是CUSTOMER的简称,第一个B指的是商品或服务的供应商,第二个B指的是从事电子商务的企业,C则是表示消费者.   ...

  5. No input file specified的解决方法apache伪静态

    http://jingyan.baidu.com/article/dca1fa6f8d623ff1a44052e8.html (一)IIS Noinput file specified 方法一:改PH ...

  6. 怎么解决dede首页网址自动加上index.html

    怎样去掉dedecms5.7(织梦)首页url后index.html有三种方法 1.去配置你的空间的默认首页地址.把index.html移到默认文本最前面.(确保你的默认文档里面有index.html ...

  7. ExtJS是一种主要用于创建前端用户界面,是一个基本与后台技术无关的前端ajax框架。

    ExtJS是一种主要用于创建前端用户界面,是一个基本与后台技术无关的前端ajax框架.

  8. J.U.C ThreadPoolExecutor解析

    Java里面线程池顶级接口是Executor,但严格意义上讲Executor并不是一个线程池,而是一个线程执行工具,真正的线程池接口是ExecutorService.关系类图如下: 首先Executo ...

  9. 如何更改MyEclipse中XML文件的字体?

    windows>Preferences>General>Appearance>Colors and Fonts>Basic>Text Font

  10. mysql-innoDB-多版本并发控制(MVCC)

    InnoDB的MVCC,是通过在每行记录后面保存三个隐藏的列来实现的其中的两个列一个保存了行的创建时间,一个保存行的过期时间(或删除时间).当然存储的并不是实际的时间值,而是系统版本号(system ...