python 使用Python和YQL获取Twitter关注者,关注和推文计数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Python和YQL获取Twitter关注者,关注和推文计数相关的知识,希望对你有一定的参考价值。

# Made for Python v3.x 
# Hacked together by Ali Almoullim.

'''
Get your Twitter profile
following, followers, and tweets count
using Python and Yahoo Query Language (YQL).
'''

#Use `pip3 install requests` to install the module.
import requests

# Your twitter username.
twitterUsername = 'alialmoullim'

# Set twitterUrl to twitter.com and append the username to it.
twitterUrl = 'https://twitter.com/' + twitterUsername
# Set yql to YQL query url.
yql = 'http://query.yahooapis.com/v1/public/yql?q='
# set the xpath value to select the followers, following,
# and tweets html elements to get their contents.
xpath = "//li/a/span[contains(@class,'ProfileNav-value')]"
# Set query to YQL query to collect the information from twitter.
query = '''select content from html where
    url=\"''' + twitterUrl + '\" and xpath=\"' + xpath + '\" limit 3'
# Finally preform the requests to to get 
res = requests.get(yql + query, params=dict(format='json')).json()

'''
The information is collected as a json format like
dictionaries and lists.
The query dict contains information about the YQL query,
the results key contains a another dict of the results form the query,
and the span key contains a list with the info selected using the query,
the first [0] is the tweets count, the second is the
following count, and the third is the followers count
'''

tweets = res['query']['results']['span'][0]
following = res['query']['results']['span'][1]
followers = res['query']['results']['span'][2]

print('Followers: ', followers)
print('Following: ', following)
print('Tweets: ', tweets)

以上是关于python 使用Python和YQL获取Twitter关注者,关注和推文计数的主要内容,如果未能解决你的问题,请参考以下文章

使用 twit npm 从 user_timeline 获取最新推文

访问 python 子列表以导入 pandas DataFrame

使用Twit包在用户资料上发布Tweet

如何使用“Twit”库从列表中选择一个随机单词

YQL查询服务现在被雅虎关闭了

Google 域的 YQL“重定向到 robots.txt 受限 URL”错误