Created
August 26, 2014 02:46
-
-
Save LYY/b0d600ebd843a8af98c1 to your computer and use it in GitHub Desktop.
用api抓微博评论
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'weibo_2' | |
| require 'csv' | |
| mids = ["aaa", "bbb", "ccc"] | |
| ids = [] | |
| token = "token" | |
| WeiboOAuth2::Config.api_key = "key" | |
| WeiboOAuth2::Config.api_secret = "secret" | |
| WeiboOAuth2::Config.redirect_uri = "http://r.com" | |
| @client = WeiboOAuth2::Client.new | |
| @client.get_token_from_hash({:access_token=>token}) | |
| def fetch_comments(id) | |
| rst = @client.comments.show(id, count: 100) | |
| comms = rst.comments | |
| while rst.next_cursor > 0 | |
| rst = fetch_comments_by_maxid id, rst.next_cursor | |
| comms.push(*rst.comments) | |
| end | |
| comms | |
| end | |
| def fetch_comments_by_maxid(id, max_id) | |
| @client.comments.show(id, count: 100, max_id: max_id) | |
| end | |
| AT_CHECK_REGX = /.*@.+@.+@.+/ | |
| def check_at(text) | |
| !!(AT_CHECK_REGX =~ text) | |
| end | |
| mids.each do |mid| | |
| id = @client.statuses.queryid mid: mid, type: 1, isBase62: 1 | |
| ids << id | |
| end | |
| ids.each_with_index do |id, index| | |
| comms = fetch_comments id | |
| name = "#{mids[index]}.csv" | |
| CSV.open("/Users/xxx/Downloads/#{name}", 'wb') do |csv| | |
| csv << ["用户昵称", "内容"] | |
| comms.each do |comm| | |
| if check_at(comm.text) | |
| csv << [comm.user.screen_name, comm.text] | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment