跳转到主要内容

无需认证即可抓取Twitter前端API。

项目描述

Twitter爬虫

GitHub GitHub contributors code size maintain status

🇰🇷 阅读韩文版

Twitter的API使用起来很麻烦,有很多限制——幸运的是,他们的前端(JavaScript)有自己的API,我对其进行了逆向工程。没有API速率限制。没有限制。极快。

您可以使用此库轻松地获取任何用户的推文。

先决条件

在您开始之前,请确保您已满足以下要求

  • 互联网连接
  • Python 3.6+

安装twitter-scraper

如果您想使用最新版本,请从源安装。要从源安装twitter-scraper,请按照以下步骤操作

Linux和macOS

git clone https://github.com/bisguzar/twitter-scraper.git
cd twitter-scraper
sudo python3 setup.py install 

您还可以使用PyPI进行安装。

pip3 install twitter_scraper

使用twitter_scraper

只需导入twitter_scraper并调用函数!

→ 函数 get_tweets(query: str [, pages: int]) -> 字典

您可以获取个人资料的推文或从标签中解析推文,get_tweets函数接受用户名或标签作为第一个参数(字符串)和要扫描的页面数作为第二个参数(整数)。

请注意

  • 如果要从标签获取推文,第一个参数需要以#(井号)开头。
  • pages参数是可选的。
Python 3.7.3 (default, Mar 26 2019, 21:43:19) 
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from twitter_scraper import get_tweets
>>> 
>>> for tweet in get_tweets('twitter', pages=1):
...     print(tweet['text'])
... 
spooky vibe check

它为每条推文返回一个字典。字典的键包括:

类型 描述
tweetId 字符串 推文的标识符,访问twitter.com/USERNAME/ID以查看推文。
userId 字符串 推文的userId
username 字符串 推文的用户名
tweetUrl 字符串 推文的URL
isRetweet 布尔值 如果是转推,则为True,否则为False
isPinned 布尔值 如果是固定推文,则为True,否则为False
time datetime 推文的发布日期
text 字符串 推文的内容
replies 整数 推文的回复数
retweets 整数 推文的转发数
likes 整数 推文的点赞数
entries 字典 包含标签、视频、照片、URLs键。每个键的值是列表

→ 函数 get_trends() -> 列表

通过调用get_trends(),您可以简单地获取您所在地区的趋势。它将返回一个字符串列表。

Python 3.7.3 (default, Mar 26 2019, 21:43:19) 
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from twitter_scraper import get_trends
>>> get_trends()
['#WHUTOT', '#ARSSOU', 'West Ham', '#AtalantaJuve', '#バビロニア', '#おっさんずラブinthasky', 'Southampton', 'Valverde', '#MMKGabAndMax', '#23NParoNacional']

→ 类 Profile(username: str) -> 类实例

您可以通过此类获取个人资料的个人资料,如生日和生物信息(如果存在且公开)。此类接受用户名参数,并返回自身。使用类变量访问信息。

Python 3.7.3 (default, Mar 26 2019, 21:43:19) 
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from twitter_scraper import Profile
>>> profile = Profile('bugraisguzar')
>>> profile.location
'Istanbul'
>>> profile.name
'Buğra İşgüzar'
>>> profile.username
'bugraisguzar'

.to_dict() -> 字典

to_dictProfile类的一个方法。将个人资料数据作为Python字典返回。

Python 3.7.3 (default, Mar 26 2019, 21:43:19) 
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from twitter_scraper import Profile
>>> profile = Profile("bugraisguzar")
>>> profile.to_dict()
{'name': 'Buğra İşgüzar', 'username': 'bugraisguzar', 'birthday': None, 'biography': 'geliştirici@peptr', 'website': 'bisguzar.com', 'profile_photo': 'https://pbs.twimg.com/profile_images/1199305322474745861/nByxOcDZ_400x400.jpg', 'banner_photo': 'https://pbs.twimg.com/profile_banners/1019138658/1555346657/1500x500', 'likes_count': 2512, 'tweets_count': 756, 'followers_count': 483, 'following_count': 255, 'is_verified': False, 'is_private': False, user_id: "1019138658"}

为twitter-scraper做出贡献

要为twitter-scraper做出贡献,请按照以下步骤操作

  1. 分叉此存储库。
  2. 创建一个具有清晰名称的分支:git checkout -b <branch_name>
  3. 做出您的更改并提交它们:git commit -m '<commit_message>'
  4. 推送到原始分支:git push origin <project_name>/<location>
  5. 创建拉取请求。

或者查看GitHub文档中的创建拉取请求

贡献者

感谢以下为该项目做出贡献的人

  • @kennethreitz(作者)
  • @bisguzar(维护者)
  • @lionking6792
  • @ozanbayram

联系

如果您想联系我,您可以通过@bugraisguzar联系我。

许可

此项目使用的许可证如下: MIT

项目详情


下载文件

下载您平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源代码分发

twitter-scraper-0.4.4.tar.gz(10.9 KB 查看哈希值

上传时间 源代码

构建分发

twitter_scraper-0.4.4-py2.py3-none-any.whl (9.5 kB 查看哈希值)

上传于 Python 2 Python 3

由以下支持