0

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

因此,我决定尝试使用网页抓取而不是使用微博 API。但是,我对此并没有太多想法。我知道我可以使用 json 和 requests 之类的库来从网站获取内容。我坚持获取内容

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(来自页面源)让我感到困惑。

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

4

1 回答 1

1

嗨,我是 python 和英语的初学者 :)。我也在做同样的事情,昨天完成了。你看到的微博页面是由浏览器中的脚本创建的。您可以通过 library re 从脚本中提取所有内容,例如“FM.view( ....”)。

登录后,您可以这样做:

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
于 2016-05-28T15:12:48.580 回答