Web scrape Weibo Follower使用python计数

时间:2016-05-18 09:28:56

标签: python weibo

您好我是python的初学者,我正在努力获得一些微博帐户的粉丝数量。我尝试过使用微博API,但我无法获得微博帐户的信息(不是我的账号/没有凭据)。根据我的查询,微博要求用户提交申请以供审核,以获得更多API(包括获取关注者数量)

因此,我决定尝试使用网页抓取而不是使用微博API。但是,我不太清楚这样做。我知道我可以使用像json这样的库和请求来获取网站上的内容。我很难获得内容

from json import loads
import requests
username_weibo = ['kupono','xxx','etc']

def get_weibo_followers(username):
    output = ['Followers']
    for user in username:
        r = requests.get('https://www.weibo.com/'+user).content
        html = r.encode('utf-8')

    return r

到目前为止,我试图打印出代码的样子,而我得到的是一堆乱七八糟的单词/字符。此外,有太多的FM.views(来自页面来源)让我感到困惑。

这是我到目前为止所做的,但我不知道如何继续。任何人都可以帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:1)

你好我是python和英语的初学者:)。我做了同样的事情并且昨天完成了。您看到的微博页面是由浏览器中的脚本创建的。你可以从像#34; FM.view(...." by。)这样的脚本中提取 图书馆重建。

登录后,您可以执行以下操作:

import re
from urllib import parse
reponse = session.get('http://weibo.com/u/xxxxxxxxx')
#xxxxxxx is the account's ID.    
html_raw_data = parse.unquote(reponse.content.decode())
#url decode
html_data = re.sub(r'\\'r'',html_raw_data)
#backslash has Escaped two times,get the raw code
follows_fans_articles_data = re.search(r'\[\'page_id\'\]=\'(\d+)',html_data,re.M)
#follows_fans_articles_data.group(1)  follows number    (2)  fans number  (3) articles number