网站调用新浪微博内容(【魔兽世界】新浪微博登录常用接口:对应主界面)

优采云 发布时间: 2022-03-16 20:07

  网站调用新浪微博内容(【魔兽世界】新浪微博登录常用接口:对应主界面)

  日志条目

  新浪微博登录常用界面:

  对应主界面:

  不过,我个人推荐使用移动端的微博入口:

  对应主界面:

  原因是手机上的数据比较轻量级,基础数据齐全,可能会缺少一些基本的个人信息,比如“*敏*感*词*完成度”、“个人等级”等。粉丝ID和关注ID只能显示20页,但可以作为大部分验证的语料库。

  通过对比下面两张图,分别是PC端和手机端,可以发现内容基本一致:

  

  如下图在手机上,图片比较小,内容也比较简洁。

  

  完整的源代码

  下面的代码主要分为三部分:

  1.登录微博(用户名、密码) 登录微博

  2.VisitPersonPage(user_id) 访问follower网站获取个人信息

  3.获取微博内容同时翻页

<p># coding=utf-8

"""

Created on 2016-02-22 @author: Eastmount

功能: 爬取新浪微博用户的信息

信息:用户ID 用户名 粉丝数 关注数 微博数 微博内容

网址:http://weibo.cn/ 数据量更小 相对http://weibo.com/

"""

import time

import re

import os

import sys

import codecs

import shutil

import urllib

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import selenium.webdriver.support.ui as ui

from selenium.webdriver.common.action_chains import ActionChains

#先调用无界面浏览器PhantomJS或Firefox

#driver = webdriver.PhantomJS(executable_path="G:phantomjs-1.9.1-windowsphantomjs.exe")

driver = webdriver.Firefox()

wait = ui.WebDriverWait(driver,10)

#全局变量 文件操作读写信息

inforead = codecs.open("SinaWeibo_List.txt", &apos;r&apos;, &apos;utf-8&apos;)

infofile = codecs.open("SinaWeibo_Info.txt", &apos;a&apos;, &apos;utf-8&apos;)

#********************************************************************************

# 第一步: 登陆weibo.cn 获取新浪微博的cookie

# 该方法针对weibo.cn有效(明文形式传输数据) weibo.com见学弟设置POST和Header方法

# LoginWeibo(username, password) 参数用户名 密码

# 验证码暂停时间手动输入

#********************************************************************************

def LoginWeibo(username, password):

try:

#**********************************************************************

# 直接访问driver.get("http://weibo.cn/5824697471")会跳转到登陆页面 用户id

#

# 用户名

# 密码 "password_4903" 中数字会变动,故采用绝对路径方法,否则不能定位到元素

#

# 勾选记住登录状态check默认是保留 故注释掉该代码 不保留Cookie 则&apos;expiry&apos;=None

#**********************************************************************

#输入用户名/密码登录

print u&apos;准备登陆Weibo.cn网站...&apos;

driver.get("http://login.weibo.cn/login/")

elem_user = driver.find_element_by_name("mobile")

elem_user.send_keys(username) #用户名

elem_pwd = driver.find_element_by_xpath("/html/body/div[2]/form/div/input[2]")

elem_pwd.send_keys(password) #密码

#elem_rem = driver.find_element_by_name("remember")

#elem_rem.click() #记住登录状态

#重点: 暂停时间输入验证码

#pause(millisenconds)

time.sleep(20)

elem_sub = driver.find_element_by_name("submit")

elem_sub.click() #点击登陆

time.sleep(2)

#获取Coockie 推荐 http://www.cnblogs.com/fnng/p/3269450.html

print driver.current_url

print driver.get_cookies() #获得cookie信息 dict存储

print u&apos;输出Cookie键值对信息:&apos;

for cookie in driver.get_cookies():

#print cookie

for key in cookie:

print key, cookie[key]

#driver.get_cookies()类型list 仅包含一个元素cookie类型dict

print u&apos;登陆成功...&apos;

except Exception,e:

print "Error: ",e

finally:

print u&apos;End LoginWeibo!

&apos;

#********************************************************************************

# 第二步: 访问个人页面http://weibo.cn/5824697471并获取信息

# VisitPersonPage()

# 编码常见错误 UnicodeEncodeError: &apos;ascii&apos; codec can&apos;t encode characters

#********************************************************************************

def VisitPersonPage(user_id):

try:

global infofile

print u&apos;准备访问个人网站.....&apos;

#原创内容 http://weibo.cn/guangxianliuyan?filter=1&page=2

driver.get("http://weibo.cn/" + user_id)

#**************************************************************************

# No.1 直接获取 用户昵称 微博数 关注数 粉丝数

# str_name.text是unicode编码类型

#**************************************************************************

#用户id

print u&apos;个人详细信息&apos;

print &apos;**********************************************&apos;

print u&apos;用户id: &apos; + user_id

#昵称

str_name = driver.find_element_by_xpath("//div[@class=&apos;ut&apos;]")

str_t = str_name.text.split(" ")

num_name = str_t[0] #空格分隔 获取第一个值 "Eastmount 详细资料 设置 新手区"

print u&apos;昵称: &apos; + num_name

#微博数 除个人主页 它默认直接显示微博数 无超链接

#Error: &apos;unicode&apos; object is not callable

#一般是把字符串当做函数使用了 str定义成字符串 而str()函数再次使用时报错

str_wb = driver.find_element_by_xpath("//div[@class=&apos;tip2&apos;]")

pattern = r"d+.?d*" #正则提取"微博[0]" 但r"([.*?])"总含[]

guid = re.findall(pattern, str_wb.text, re.S|re.M)

print str_wb.text #微博[294] 关注[351] 粉丝[294] 分组[1] @他的

for value in guid:

num_wb = int(value)

break

print u&apos;微博数: &apos; + str(num_wb)

#关注数

str_gz = driver.find_element_by_xpath("//div[@class=&apos;tip2&apos;]/a[1]")

guid = re.findall(pattern, str_gz.text, re.M)

num_gz = int(guid[0])

print u&apos;关注数: &apos; + str(num_gz)

#粉丝数

str_fs = driver.find_element_by_xpath("//div[@class=&apos;tip2&apos;]/a[2]")

guid = re.findall(pattern, str_fs.text, re.M)

num_fs = int(guid[0])

print u&apos;粉丝数: &apos; + str(num_fs)

#***************************************************************************

# No.2 文件操作写入信息

#***************************************************************************

infofile.write(&apos;=====================================================================

&apos;)

infofile.write(u&apos;用户: &apos; + user_id + &apos;

&apos;)

infofile.write(u&apos;昵称: &apos; + num_name + &apos;

&apos;)

infofile.write(u&apos;微博数: &apos; + str(num_wb) + &apos;

&apos;)

infofile.write(u&apos;关注数: &apos; + str(num_gz) + &apos;

&apos;)

infofile.write(u&apos;粉丝数: &apos; + str(num_fs) + &apos;

&apos;)

infofile.write(u&apos;微博内容: &apos; + &apos;

&apos;)

#***************************************************************************

# No.3 获取微博内容

# http://weibo.cn/guangxianliuyan?filter=0&page=1

# 其中filter=0表示全部 =1表示原创

#***************************************************************************

print &apos;

&apos;

print u&apos;获取微博内容信息&apos;

num = 1

while num

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线