Twint 是一个用 Python 写的 Twitter 抓取工具, 允许从 Twitter 配置文件中抓取推文, 不使用 Twitter 的 API.
Twint 利用 Twitter 的搜索语法让您从特定用户那里搜索推文, 特定主题, 主题标签和相关的推文, 或者从推文中挑选敏感信息, 如电子邮件和电话号码.
Twint 还对 Twitter 进行了特殊查询, 允许您搜索 Twitter 用户的关注者, 用户喜欢的推文, 以及他们在 API,Selenium 或模拟浏览器的情况下关注的用户.
好处
使用 Twint 和 Twitter API 的一些好处:
1. 可以获取几乎 所有的 推文 (Twitter API 限制只能持续 3200 个推文);
2. 快速初始设置;
3. 可以匿名使用, 无需 Twitter 注册;
4. 没有速率限制.
Twitter 的限制
Twitter 会限制用户可以浏览的时间线. 这意味着通过. Profile 或者. Favorites 你只可以看到~ 3200 条推文.
要求
- Python 3.6;
- aiohttp;
- aiodns;
- beautifulsoup4;
- cchardet;
- Elasticsearch;
- pysocks;
- pandas (> = 0.23.0);
- aiohttp_socks;
- schedule;
- geopy;
- fake-useragent.
安装
Git 的方法:
- Git clone https://github.com/twintproject/twint.git
- pip3 install -r requirements.txt
- PIP:
- pip3 install twint
要么
- pip3 install --user --upgrade -e Git + https//GitHub.com/twintproject/twint.Git@origin/master#egg=twint
- Pipenv:
- pipenv install -e Git + https://github.com/twintproject/twint.git#egg=twint
CLI 基本示例和组合
一些简单的例子可以帮助您掌握基础知识:
1.twint -u username- 从用户的时间线中删除所有推文.
2.twint -u username -s pineapple- 从包含 pineapple 的用户时间线中删除所有推文.
3.twint -s pineapple- 从每个人的推文收集每个包含 pineapple 的推文.
4.twint -u username -year 2014- 收集 2014 年 之前发送 推文的推文.
5.twint -u username -since 2015-12-20 - 收集自 2015-12-20 以来推文的推文.
6.twint -u username -o file.txt - 抓取推文并保存到 file.txt.
7.twint -u username -o file.CSV -CSV - 抓取推文并保存为 CSV 文件.
8.twint -u username -email -phone - 显示可能包含电话号码或电子邮件地址的推文.
9.twint -s "Donald Trump" -verified - 由经过验证的用户显示有关唐纳德特朗普的推文.
10.twint -g="48.880048,2.385939,1km" -o file.CSV -CSV - 在巴黎一个地方绕半径 1 公里的推文将它们导出到 CSV 文件中.
11.twint -u username -es localhost:9200 - 输出推文到 Elasticsearch 数据库中
12.twint -u username -o file.JSON -JSON - 抓取推文并保存为 JSON 文件.
13.twint -u username -database tweets.db - 将推文保存到 SQLite 数据库.
14.twint -u username -followers - Twitter 用户的粉丝.
15.twint -u username -following - Twitter 用户关注的人.
16.twint -u username -favorites - 收集用户最喜欢的所有推文 (收集~ 3200 推文).
17.twint -u username -following -user-full - 收集一个人关注的完整用户信息
18.twint -u username -profile-full - 使用缓慢但有效的方法从用户的个人资料中收集推文 (收集~ 3200 推文, 包括转推).
19.twint -u username -retweets - 使用快速方法从用户的个人资料中收集最近的 900 个推文 (包括转推).
20.twint -u username -resume resume_file.txt - 从上次保存的 scroll-id 开始恢复搜索.
有关命令和选项的更多详细信息位于 Wiki 中
模块示例
Twint 可以用作模块并支持自定义格式.
- import twint
- # Configure
- c = twint.Config()
- c.Username = "noneprivacy"
- c.Search = "#osint"
- c.Format = "Tweet id: {id} | Tweet: {tweet}"
- # Run
- twint.run.Search(c)
输出
- 955511208597184512 2018-01-22 18:43:19 GMT <now> pineapples are the best fruit
- import twint
- c = twint.Config()
- c.Username = "noneprivacy"
- c.Custom["tweet"] = ["id"]
- c.Custom["user"] = ["bio"]
- c.Limit = 10
- c.Store_csv = True
- c.Output = "none"
- twint.run.Search(c)
存储选项
写入文件;
- CSV;
- JSON;
- SQLite;
- Elasticsearch.
Elasticsearch 设置
有关使用 Twint 设置 Elasticsearch 的详细信息位于 Wiki 中 .
图形可视化
图表 https://github.com/twintproject/twint/wiki/Graph 详细信息也位于 wiki 中 https://github.com/twintproject/twint/wiki/Graph .
我们正在开发 Twint 桌面应用程序.
常问问题
我尝试从用户那里抓取推文, 我知道它们存在, 但我没有得到它们.
Twitter 可以禁止影子账户, 这意味着他们的推文不会通过搜索获得. 要解决此问题,-profile-full 请通过 CLI 使用 Twint, 如果使用 Twint 作为模块, 则添加 config.Profile_full = True. 请注意, 此过程将非常缓慢.
更多例子
仅获取关注者用户名 / 以下用户名
- twint -u username --followers
- twint -u username --following
获取关注者 / 关注用户的用户信息
- twint -u username --followers --user-full
- twint -u username --following --user-full
用户列表
仅获取用户的用户信息
twint -u username --user-full
从用户列表中获取用户的用户信息
twint --userlist inputlist --user-full
来源: http://www.tuicool.com/articles/yaeQrmI