抓取代码:

# coding=utf-8
import os
import re
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
import IniFile
class weibo: def __init__(self):
#通过配置文件获取IEDriverServer.exe路径
configfile = os.path.join(os.getcwd(),'config.conf')
cf = IniFile.ConfigFile(configfile)
IEDriverServer = cf.GetValue("section", "IEDriverServer")
#每抓取一页数据延迟的时间,单位为秒,默认为5秒
self.pageDelay = 5
pageInteralDelay = cf.GetValue("section", "pageInteralDelay")
if pageInteralDelay:
self.pageDelay = int(pageInteralDelay) os.environ["webdriver.ie.driver"] = IEDriverServer
self.driver = webdriver.Ie(IEDriverServer) def scroll_top(self):
'''
滚动条拉到顶部
:return:
'''
if self.driver.name == "chrome":
js = "var q=document.body.scrollTop=0" else:
js = "var q=document.documentElement.scrollTop=0"
return self.driver.execute_script(js) def scroll_foot(self):
'''
滚动条拉到底部
:return:
''' if self.driver.name == "chrome":
js = "var q=document.body.scrollTop=10000" else:
js = "var q=document.documentElement.scrollTop=10000"
return self.driver.execute_script(js) def printTopic(self,topic):
print '原始数据: %s' % topic
print ' '
author_time_nums_index = topic.rfind('@')
ht = topic[:author_time_nums_index]
ht = ht.replace('\n', '')
print '话题: %s' % ht author_time_nums = topic[author_time_nums_index:]
author_time = author_time_nums.split('ñ')[0]
nums = author_time_nums.split('ñ')[1]
pattern1 = re.compile(r'\d{1,2}分钟前|今天\s{1}\d{2}:\d{2}|\d{1,2}月\d{1,2}日\s{1}\d{2}:\d{2}')
time1 = re.findall(pattern1, author_time) print '话题作者: %s' % author_time.split(' ')[0]
# print '时间: %s' % author_time.split(' ')[1]
print '时间: %s' % time1[0]
print '点赞量: %s' % nums.split(' ')[0]
print '评论量: %s' % nums.split(' ')[1]
print '转发量: %s' % nums.split(' ')[2]
print ' ' def CatchData(self,listClass,firstUrl):
'''
抓取数据
:param id: 要获取元素标签的ID
:param firstUrl: 首页Url
:return:
'''
start = time.clock()
#加载首页
wait = ui.WebDriverWait(self.driver, 20)
self.driver.get(firstUrl)
#打印标题
print self.driver.title # # 聚焦元素
# target = self.driver.find_element_by_id('J_ItemList')
# self.driver.execute_script("arguments[0].scrollIntoView();", target) #滚动5次滚动条
Scrollcount = 5
while Scrollcount > 0:
Scrollcount = Scrollcount -1
self.scroll_foot() #滚动一次滚动条,定位查找一次
total = 0
for className in listClass:
time.sleep(10)
wait.until(lambda driver: self.driver.find_elements_by_xpath(className))
Elements = self.driver.find_elements_by_xpath(className)
for element in Elements:
print ' '
txt = element.text.encode('utf8')
self.printTopic(txt)
total = total + 1 self.driver.close()
self.driver.quit()
end = time.clock() print ' '
print "共抓取了: %d 个话题" % total
print "整个过程用时间: %f 秒" % (end - start) # #测试抓取微博数据
obj = weibo()
#pt_li pt_li_2 S_bg2
#pt_li pt_li_1 S_bg2
# firstUrl = "http://weibo.com/?category=0"
firstUrl = "http://weibo.com/?category=1760"
listClass = []
listClass.append("//li[@class='pt_li pt_li_1 S_bg2']")
listClass.append("//li[@class='pt_li pt_li_2 S_bg2']")
obj.CatchData(listClass,firstUrl)

 登录窗口

  

  

 def longon(self):

        flag = True
try: self.driver.get('https://weibo.com/')
self.driver.maximize_window()
time.sleep(2)
accname = self.driver.find_element_by_id("loginname") accname.send_keys('username') accpwd = self.driver.find_element_by_name("password")
accpwd.send_keys('password')
submit = self.driver.find_element_by_xpath("//div[@class='info_list login_btn']/a")
submit.click()
time.sleep(2)
except Exception as e1:
message = str(e1.args)
flag = False
return flag

[Python爬虫] 之四:Selenium 抓取微博数据的更多相关文章

  1. [Python爬虫] 之八:Selenium +phantomjs抓取微博数据

    基本思路:在登录状态下,打开首页,利用高级搜索框输入需要查询的条件,点击搜索链接进行搜索.如果数据有多页,每页数据是20条件,读取页数 然后循环页数,对每页数据进行抓取数据. 在实践过程中发现一个问题 ...

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

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

  3. 如何让Python爬虫一天抓取100万张网页

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 王平 源自:猿人学Python PS:如有需要Python学习资料的 ...

  4. 一个月入门Python爬虫,轻松爬取大规模数据

    Python爬虫为什么受欢迎 如果你仔细观察,就不难发现,懂爬虫.学习爬虫的人越来越多,一方面,互联网可以获取的数据越来越多,另一方面,像 Python这样的编程语言提供越来越多的优秀工具,让爬虫变得 ...

  5. 芝麻HTTP:Python爬虫实战之抓取爱问知识人问题并保存至数据库

    本次为大家带来的是抓取爱问知识人的问题并将问题和答案保存到数据库的方法,涉及的内容包括: Urllib的用法及异常处理 Beautiful Soup的简单应用 MySQLdb的基础用法 正则表达式的简 ...

  6. python爬虫——用selenium爬取京东商品信息

    1.先附上效果图(我偷懒只爬了4页)  2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...

  7. python爬虫 前程无忧网页抓取

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  8. Python爬虫:如何爬取分页数据?

    上一篇文章<Python爬虫:爬取人人都是产品经理的数据>中说了爬取单页数据的方法,这篇文章详细解释如何爬取多页数据. 爬取对象: 有融网理财项目列表页[履约中]状态下的前10页数据,地址 ...

  9. Python爬虫之一 PySpider 抓取淘宝MM的个人信息和图片

    ySpider 是一个非常方便并且功能强大的爬虫框架,支持多线程爬取.JS动态解析,提供了可操作界面.出错重试.定时爬取等等的功能,使用非常人性化. 本篇通过做一个PySpider 项目,来理解 Py ...

随机推荐

  1. ArrayBuffer对象、TypedArray视图和DataView视图

    转:http://es6.ruanyifeng.com/#docs/arraybuffer ArrayBuffer ArrayBuffer 对象 TypedArray 视图 复合视图 DataView ...

  2. 洛谷P3812 【模板】线性基 [线性基]

    题目传送门 线性基 题目描述 给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大. 输入输出格式 输入格式: 第一行一个数n,表示元素个数 接下来一行n个数 输出格式: 仅一行 ...

  3. redis和mySql的数据同步的解析

    1.同步MySQL数据到Redis (1) 在redis数据库设置缓存时间,当该条数据缓存时间过期之后自动释放,去数据库进行重新查询,但这样的话,我们放在缓存中的数据对数据的一致性要求不是很高才能放入 ...

  4. php 5.6 安装openssl extension 出现编译错误

    废话不多说,直接上问题: PHP和openssl extension都是最新版本的,标准步骤安装时候出现如下问题: php:php-5.6.27 openssl:openssl-1.1.0e ==== ...

  5. Redis学习篇(二)之Hash类型及其操作

    HSET 作用: 将哈希表key中的域field设置成指定的value 语法:HSET key field value HSET userinfo name 'zhangsan' HSET useri ...

  6. android 同一个service启动之后 能不能被绑定bind

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 可以 startService 启动了一个服务,这个服务可以再调用 bindServic ...

  7. NOI2005 维护数列(splay)

    学了半天平衡树,选择了一道题来写一写,发现题目是裸的splay模板,但是还是写不好,这个的精髓之处在于在数列的某一个位置加入一个数列,类似于treap里面的merge,然后还学到了题解里面的的回收空间 ...

  8. 「SCOI2014」方伯伯的商场之旅

    「SCOI2014」方伯伯的商场之旅 题目描述 方伯伯有一天去参加一个商场举办的游戏.商场派了一些工作人员排成一行.每个人面前有几堆石子.说来也巧,位置在 \(i\) 的人面前的第 \(j\) 堆的石 ...

  9. [CODE FESTIVAL 2018]Sushi Restaurant

    题意:有$n$个人,对每个人,他有$p_i$的概率饥饿值为$x_i$($1\leq i\leq m$),你现在要做$n$盘寿司,每盘寿司有一定的数量,当这$n$个人的饥饿值确定后他们会自己选择最优的( ...

  10. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...