twitter-scrapper/README.md

182 lines
3.6 KiB
Markdown
Raw Normal View History

2018-11-29 17:33:44 +02:00
# Twitter Scraper
2021-03-09 10:54:25 +02:00
[![Go Reference](https://pkg.go.dev/badge/github.com/n0madic/twitter-scraper.svg)](https://pkg.go.dev/github.com/n0madic/twitter-scraper)
2018-11-29 17:33:44 +02:00
Twitter's API is annoying to work with, and has lots of limitations —
luckily their frontend (JavaScript) has it's own API, which I reverse-engineered.
No API rate limits. No tokens needed. No restrictions. Extremely fast.
You can use this library to get the text of any user's Tweets trivially.
2020-06-15 15:05:41 +03:00
## Installation
```shell
go get -u github.com/n0madic/twitter-scraper
```
2018-11-29 17:33:44 +02:00
## Usage
### Get user tweets
2019-09-21 10:59:45 +03:00
2018-11-29 17:33:44 +02:00
```golang
package main
import (
2020-06-15 15:19:14 +03:00
"context"
2018-11-29 17:33:44 +02:00
"fmt"
twitterscraper "github.com/n0madic/twitter-scraper"
)
func main() {
2020-12-12 23:33:57 +02:00
scraper := twitterscraper.New()
for tweet := range scraper.GetTweets(context.Background(), "Twitter", 50) {
2019-09-21 11:02:22 +03:00
if tweet.Error != nil {
panic(tweet.Error)
}
fmt.Println(tweet.Text)
2018-11-29 17:33:44 +02:00
}
}
```
It appears you can ask for up to 50 tweets (limit ~3200 tweets).
2018-11-29 17:33:44 +02:00
2021-03-09 10:40:22 +02:00
### Get single tweet
```golang
package main
import (
"fmt"
twitterscraper "github.com/n0madic/twitter-scraper"
)
func main() {
scraper := twitterscraper.New()
tweet, err := scraper.GetTweet("1328684389388185600")
if err != nil {
panic(err)
}
fmt.Println(tweet.Text)
}
```
2020-05-15 17:56:57 +02:00
### Search tweets by query standard operators
Tweets containing “twitter” and “scraper” and “data“, filtering out retweets:
```golang
package main
import (
2020-06-15 15:19:14 +03:00
"context"
"fmt"
twitterscraper "github.com/n0madic/twitter-scraper"
)
func main() {
2020-12-12 23:33:57 +02:00
scraper := twitterscraper.New()
for tweet := range scraper.SearchTweets(context.Background(),
2020-06-15 14:56:52 +03:00
"twitter scraper data -filter:retweets", 50) {
if tweet.Error != nil {
panic(tweet.Error)
}
fmt.Println(tweet.Text)
}
}
```
2020-12-04 15:08:33 +07:00
The search ends if we have 50 tweets.
2020-12-23 19:53:48 +02:00
See [Rules and filtering](https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators) for build standard queries.
#### Set search mode
```golang
2020-12-23 19:53:48 +02:00
scraper.SetSearchMode(twitterscraper.SearchLatest)
```
2020-12-23 19:53:48 +02:00
Options:
* `twitterscraper.SearchTop` - default mode
* `twitterscraper.SearchLatest` - live mode
* `twitterscraper.SearchPhotos` - image mode
* `twitterscraper.SearchVideos` - video mode
2019-09-21 10:59:45 +03:00
### Get profile
```golang
package main
import (
2019-09-21 11:02:22 +03:00
"fmt"
twitterscraper "github.com/n0madic/twitter-scraper"
2019-09-21 10:59:45 +03:00
)
func main() {
2020-12-12 23:33:57 +02:00
scraper := twitterscraper.New()
profile, err := scraper.GetProfile("Twitter")
2019-09-21 11:02:22 +03:00
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", profile)
2019-09-21 10:59:45 +03:00
}
```
2020-02-12 10:45:19 +02:00
### Get trends
```golang
package main
import (
"fmt"
twitterscraper "github.com/n0madic/twitter-scraper"
)
func main() {
2020-12-12 23:33:57 +02:00
scraper := twitterscraper.New()
trends, err := scraper.GetTrends()
2020-02-12 10:45:19 +02:00
if err != nil {
panic(err)
}
fmt.Println(trends)
}
```
### Use http proxy
```golang
2020-12-12 23:33:57 +02:00
err := scraper.SetProxy("http://localhost:3128")
if err != nil {
panic(err)
}
```
### Load timeline with tweet replies
```golang
2020-12-12 23:33:57 +02:00
scraper.WithReplies(true)
```
2020-12-12 23:33:57 +02:00
### Default Scraper (Ad hoc)
In simple cases, you can use the default scraper without creating an object instance
```golang
import twitterscraper "github.com/n0madic/twitter-scraper"
// for tweets
twitterscraper.GetTweets(context.Background(), "Twitter", 50)
// for tweets with replies
twitterscraper.WithReplies(true).GetTweets(context.Background(), "Twitter", 50)
// for search
twitterscraper.SearchTweets(context.Background(), "twitter", 50)
// for profile
twitterscraper.GetProfile("Twitter")
// for trends
twitterscraper.GetTrends()
```