新浪围脖私信导出脚本

最近因为需要写了一个山寨的围脖私信导出脚本。Run了一个小时,导出了近两万条私信,效果还算可以。Google一下网上没人贴这种工具,所以野人献曝一下。具体的技术细节放在英文的blog上。测试环境是Ubuntu 11.11,需要用pip安装selenium,然后再去Chromium的页面下载Chrome的WebDriver binary。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 –*-
# A sina weibo DM export tool
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time
import codecs
 
f = codecs.open('weibo.txt', encoding='utf-8', mode='w+')
 
# Create a new instance of the Chrome
driver = webdriver.Chrome()
# Set 15 sec as default timeout (maximum waiting time if something can't be found)
driver.implicitly_wait(15)
# go to the direct message history page (for DM with one user)
driver.get("http://weibo.com/message/history?uid=xxxxxxxxxx")
 
# Find loginname input box
loginnameInput = driver.find_element_by_id("loginname")
loginnameInput.send_keys("me@mydomain.com")
# Find password input box
passwdInput = driver.find_element_by_id("password")
passwdInput.send_keys("mypasswd")
# Find the submit button
submitButton = driver.find_element_by_id("login_submit_btn")
# Submit
submitButton.click()
 
n = 1
more = 1
while more:
    # Find message box
    messages = driver.find_elements_by_class_name("txt")
    # Find time tag box
    ts = driver.find_elements_by_css_selector("em.W_textb.date")
    f.write(ts[-1].text + "\n")
    for msg in reversed(messages):
        if (msg.text != ''):
            f.write(msg.text + "\n")
    f.flush()
    buttons = driver.find_elements_by_class_name("W_btn_a")
    more = 0
    for button in buttons:
        # Next page or previous page
        if button.text == u'上一页':
            more = 1
            break
    if more:
        n += 1
        print 'Page %d' % n
        button.click()
        time.sleep(2)
f.close()
print 'All Done!'
driver.quit()

这个脚本稍加改动应该就可以做成自动化的工具,不过懒得去折腾了,就搁这吧。

  • yuxinch

    两万条私信!!!!哇塞狗狗你的新浪这么忙啊。。

  • Weizhe ZHANG

    嘿嘿,此中有真意啊~

  • ganxuming

    你好楼主, Ubuntu的确实不会,专业性的技术操作多,能方便帮一个忙么?就是我想把和女友的近2000条私信导出。然后基本主要去twitter

    期待回复

  • http://blog.cybersnoopy.com/ CyberSnoopy

    呃,这个需要你的帐户和密码,还有你和你女友私信页面的那个message id……(http://weibo.com/message/history?uid=xxxxxxxxxx)

  • http://blog.cybersnoopy.com/ CyberSnoopy

    hiahia

  • ganxuming

    太棒了这么快reply,方便的话用邮箱告之。

    真心感谢你的帮助

  • Ganner

    楼主你好,最近女友不慎将其私信删除,只有我这有,想将1w多条私信导出,找到了这里,我有基础编程经验,还请楼主指教,帮我搭建平台可否?

  • http://blog.cybersnoopy.com/ CyberSnoopy

    你好,如果有linux环境的话,稍微改动上面的脚本里的用户、密码以及链接名就可以,脚本在这里 https://docs.google.com/file/d/0BzKZ4d4SB3ARb2FLM3doT3RlVzQ/edit?usp=sharing

  • Ganner

    好的,谢谢,我没有linux还需要现下载现安装,主要我对linux和python都不了解,不过我可以现学,之后有问题还请楼主不吝赐教啊

  • http://blog.cybersnoopy.com/ CyberSnoopy

    没问题,有编程基础就好办~好运~

  • Ganner

    嗯,谢谢!

  • Ganner

    楼主,我搭好了平台,在尝试运行程序的时候提示“driver.get(& quot;http://weibo.com/message/history?uid=16821***** & quot;)”这句的& quot(加了空格不然会被识别转换)有语法错误,我猜测是因为我的Chrome的WebDriver binary没装好,因为没找到你说的Chromium的页面下载的地方,我在Ubuntu的软件中心搜索到了Chromium-chromeddriver(25.0.1364.160-0ubuntu3)并安装了,估计装的不对,还请帮忙告诉Chrome的WebDriver binary应该怎么装。

    P.S. 我装的Ubuntu13.04,使用Eclipse安装了PyDev作编程环境,在终端控制台使用命令行安装了pip,并用pip安装了selenium

    谢谢~

  • http://blog.cybersnoopy.com/ CyberSnoopy

    你可以去这里看下流程 https://code.google.com/p/selenium/wiki/ChromeDriver Driverdownload的地址在这里,https://code.google.com/p/chromedriver/downloads/list。不过你的出错信息可以先贴一下么。

  • Ganner

    谢谢您的及时回复,不好意思,我一上班就不太有时间弄自己的事,这么晚才来贴。我找到过你给的下载地址,并下载了chromedriver_linux64_26.0.1383.0.zip这个压缩包,但我不知道怎么安装它。。
    看了你给的流程的链接,我发现我在/usr/bin/路径下没有google-chrome,我安装的是开源版的Chromium,在软件中心安装的。

    上回问的quot的问题我解决了,看了你给的那个流程的链接,知道那应该是个实体的引号,因为我用的是你贴在博客里的代码,后来我去下了你给的代码文件。

    现在有了新的问题,还麻烦帮忙看一下:

    1.代码里的”if
    button.text ==
    u’下一页’:
    ”中的“下一页”,执行的时候报错说是非ASCII码值,是不是我文件的存储格式的问题,utf-8那个

    2.我把“下一页”三个字随便写了几个英文,先过去这个错误,运行报错,应该是chromedriver没安装好,您帮我看看:

    Traceback
    (most recent call last):

    File
    “HelloWorld.py”, line 25, in

    driver
    = webdriver.Chrome()

    File
    “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py”,
    line 59, in __init__

    self.service.start()

    File
    “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/service.py”,
    line 68, in start

    and
    read up at http://code.google.com/p/selenium/wiki/ChromeDriver“)

    selenium.common.exceptions.WebDriverException:
    Message: ‘ChromeDriver executable needs to be available in the path.
    Please download from
    http://code.google.com/p/selenium/downloads/list and
    read up at http://code.google.com/p/selenium/wiki/ChromeDriver

    非常感谢!

    回复特别长,如果影响了你的评论列表,还请麻烦邮件联系,我的邮箱:GannerDay#163.com

  • Ganner

    啊哈,我刚刚找到了如何安装chromedriver了,现在代码可以运行了,但是弹出的Chrome卡在微博的的登陆界面那没反应了。。。终端提示找不到“loginname”

  • Ganner

    已经成功导出了,非常感谢!
    下一页报错的问题我把你的utf-8的coding声明提到最前面就好了
    登陆不了那个没解决,我用断点暂停,然后手动输入的方式用的
    我觉得可能是新浪把登陆框改了,不过我没在网页代码里找见登陆框的名字,还麻烦兄台帮忙看一下是为什么。

    绕过登陆后发现新浪网页代码中时间戳的代码名字及上一页按钮的代码名字都变了,根据源代码中新的名字修改后就能用了。
    再次感谢!