Giter VIP home page Giter VIP logo

weibospider's Introduction

WeiboSpider

Python selenium
利用selenium实现下列三个功能:
1.后台自动爬取指定关键词下发布的所有微博及其相关数据;
2.爬取指定时间段的微博关键词搜索结果;
3.根据用户主页爬取用户基本信息.
具体使用方法请查看 3.示例

更新日志

▽ 2019.05.21更新

blogSpider.pyweibo_spider函数中添加了爬取指定时间段下的关键词搜索结果功能

▽ 2019.04.19更新

更新了用于爬取用户基本信息的程序userSpider.py, 并将原爬虫程序Spider.py更名为blogSpider.py

▽ 2019.01.29更新

更新了账号登录函数weibo_login, 更新后可以输入任意多次的验证码, 当系统不要求再次输入验证码后, 需要在python界面中输入字符——N 程序才会继续运行
example
优:能够应对任意多次验证码输入请求
劣:无法自行判断是否需要再次输入验证码, 需要人工帮助判断

程序说明

1.注意事项

  1. 目前该程序只能在FirefoxGoogle Chrome上运行, 运行程序前请确保计算机已经安装了其中一款浏览器,并下载对应的浏览器驱动, 将驱动(.exe文件)与python.exe放置在同一文件夹中, 这里给出了两种64位windows下的浏览器驱动的压缩文件, 下表给出了各系统下的浏览器驱动下载链接

    浏览器名称 驱动名
    Firefox geckodriver.exe
    Chrome chromedriver.exe

2.爬取内容

2.1 blogSpider

内容 变量名 数据类型
用户名 ID str
用户主页 Href str
微博博文 Blog str
发布时间 PubTime str
点赞数 Like int
评论数 Comment int
转发数 Transfer int

2.2 userSpider

内容(变量名) 数据类型 内容(变量名) 数据类型
昵称 str 血型 str
真实姓名 str 博客地址 str
所在地 str 个性域名 str
性别 str 简介 str
性取向 str 注册时间 str
感情状况 str href str
生日 str

3.示例

3.1 blogSpider

先爬取关键词微博下的前20页微博信息, 接着每隔半小时(1800s)更新一次数据

csv_file = 'C:/test.csv'
my_username = 'abcdefg'
my_password = '11111111'
keyword = '微博'
my_browser = 'Firefox'
my_scope = 'origin'  

# 待机爬取示例
Standby(keyword, csv_file, my_username, my_password, maxpage=50, sleeptime=1800, browser=my_browser)

# 按指定时间段爬取示例
# 1.爬取2019年5月1日至5月7日的全部搜索结果
time_start = '2019-05-01'
time_end = '2019-05-07'
driver, weibo_result = weibo_spider(keyword=keyword,
                                    username=my_username,
                                    password=my_password,
                                    time_from=time_start,
                                    time_to=time_end,
                                    scope='all')
latest, result = select_data(weibo_result, login=True, filepath=csv_file)
save_blog(result, csv_file)
print(weibo_result)

# 2.爬取2019年5月1日09:00至10:00的原创微博搜索结果
time_start = '2019-05-01-09'
time_end = '2019-05-07-10'
driver, weibo_result = weibo_spider(keyword=keyword, 
                                    username=my_username,
                                    password=my_password,
                                    time_from=time_start,
                                    time_to=time_end,
                                    scope='origin')
latest, result = select_data(weibo_result, login=True, filepath=csv_file)
save_blog(result, csv_file)
print(weibo_result)

3.2 userSpider

可输入单个用户主页或用户主页列表(程序目前仅可识别list/array/tuple)
主函数userinfo中有两个bool类型参数:savednewfile, 分别代表是否将爬取的数据保存为csv格式数据、保存的文件是否覆盖原文件/创建新文件
注意: saved与newfile只要其中一个为True, 输入中都应包括filepath
爬取单个用户主页并仅返回爬取数据:

myhref = 'https://weibo.com/weibokefu'
myusername = '123456'
mypassword = '123456'
only_data = userinfo(myhref, myusername, mypassword)  # 仅获取爬取的数据

爬取E:/hrefs.csv中的多个用户主页并将数据保存到C盘:

import csv

myusername = '123456'
mypassword = '123456'
myfilepath = 'C:/test.csv'
f = csv.reader(open('E:/hrefs.csv', encoding='utf-8'))
myhrefs = [href for href in f]
mybrowser = 'Chrome'  # 默认使用Firefox

userinfo(myhrefs, myusername, mypassword, browser=mybrowser, filepath=myfilepath, saved=True, newfilw=True)

weibospider's People

Contributors

qinyuenlp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

weibospider's Issues

可以爬取固定时间内发表的微博吗

您好,现在的代码获取某关键字下搜到的微博是从时间最新的开始,而且搜索结果好像只有50页,那是不是意味着50页以前的就无法爬取了呢?如果我想获得固定时间段的微博比如2018年间的,可以对这个代码进行修改吗?我看了一下好像很多地方都和最近时间latest相关,好像不太好改,请教您一下。

可以获取用户信息吗

您好,请问可以把用户的个人信息如性别/所在地/出生年月等标签一起爬取下来吗

博主你好,我运行你的程序提示ssl 错误

[8180:13216:0213/234452.856:ERROR:gcm_channel_status_request.cc(145)] GCM channel request failed.
[19588:19716:0213/234510.090:ERROR:ssl_client_socket_impl.cc(941)] handshake failed; returned -1, SSL error code 1, net_error -101
前两天还可以爬的,现在不行了。。这种情况是我的ip被封了还是怎么回事呀?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.