前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Selenium抓取QQ空间好友说说1.安装Selenium2.在Python中使用Selenium获取QQ空间好友说说3.代码实现(基于Python3)

使用Selenium抓取QQ空间好友说说1.安装Selenium2.在Python中使用Selenium获取QQ空间好友说说3.代码实现(基于Python3)

作者头像
Python攻城狮
发布2018-08-23 12:06:13
1.6K0
发布2018-08-23 12:06:13
举报
文章被收录于专栏:Python攻城狮Python攻城狮

代码参考http://www.jianshu.com/p/a6769dccd34d 刚接触Selenium的戳这里Selenium与PhantomJS PS:代码的不足在于只能抓取第一页的说说内容,代码的改进之处在于增加了与数据库的交互,进行了存储

1.安装Selenium

代码语言:javascript
复制
pip install Selenium

2.在Python中使用Selenium获取QQ空间好友说说

分析网页结构

说说内容

发表说说的时间

3.代码实现(基于Python3)

代码语言:javascript
复制
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pymongo

# #使用Selenium的webdriver实例化一个浏览器对象,在这里使用Phantomjs
# driver = webdriver.PhantomJS(executable_path=r"D:\phantomjs-2.1.1-windows\bin\phantomjs.exe")
# #设置Phantomjs窗口最大化
# driver.maximize_window()


# 登录QQ空间
def get_shuoshuo(qq):
    #建立与MongoClient的链接
    client = pymongo.MongoClient('localhost', 27017)
    #得到数据库
    db = client['shuoshuo']
    #得到一个数据集合
    sheet_tab = db['sheet_tab']

    chromedriver = r"E:\mycode\chromedriver.exe"
    driver = webdriver.Chrome(chromedriver)
    #使用get()方法打开待抓取的URL
    driver.get('http://user.qzone.qq.com/{}/311'.format(qq))
    time.sleep(5)
    #等待5秒后,判断页面是否需要登录,通过查找页面是否有相应的DIV的id来判断
    try:
        driver.find_element_by_id('login_div')
        a = True
    except:
        a = False
    if a == True:
        #如果页面存在登录的DIV,则模拟登录
        driver.switch_to.frame('login_frame')
        driver.find_element_by_id('switcher_plogin').click()
        driver.find_element_by_id('u').clear()  # 选择用户名框
        driver.find_element_by_id('u').send_keys('QQ号')
        driver.find_element_by_id('p').clear()
        driver.find_element_by_id('p').send_keys('QQ密码')
        driver.find_element_by_id('login_button').click()
        time.sleep(3)
    driver.implicitly_wait(3)

    #判断好友空间是否设置了权限,通过判断是否存在元素ID:QM_OwnerInfo_Icon
    try:
        driver.find_element_by_id('QM_OwnerInfo_Icon')
        b = True
    except:
        b = False
    #如果有权限能够访问到说说页面,那么定位元素和数据,并解析
    if b == True:
        driver.switch_to.frame('app_canvas_frame')
        content = driver.find_elements_by_css_selector('.content')
        stime = driver.find_elements_by_css_selector('.c_tx.c_tx3.goDetail')
        for con, sti in zip(content, stime):
            data = {
                'time': sti.text,
                'shuos': con.text
            }
            print(data)
            sheet_tab.insert_one(data)
        pages = driver.page_source
        soup = BeautifulSoup(pages, 'lxml')

    #尝试一下获取Cookie,使用get_cookies()
    cookie = driver.get_cookies()
    cookie_dict = []
    for c in cookie:
        ck = "{0}={1};".format(c['name'], c['value'])
        cookie_dict.append(ck)
    i = ''
    for c in cookie_dict:
        i += c
    print('Cookies:', i)


    driver.close()
    driver.quit()


if __name__ == '__main__':
    get_shuoshuo('好友的QQ号')

注意:使用前记得安装chromedriver这个插件,使用的过程中会呼起一个谷歌浏览器。如果写了绝对路径还报错的话,就加入环境变量。

通过Robo 3T(数据库MongoDB的一款功能强大的数据库管理工具)可以看到我们已经将拿到的数据库存储于数据库中

接下来我们应该通过拿到的数据做一些数据分析...可是我不会!!!

正在努力学习数据分析中.....

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.10.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.安装Selenium
  • 2.在Python中使用Selenium获取QQ空间好友说说
  • 3.代码实现(基于Python3)
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档