Skip to content

Commit e378f08

Browse files
Add .env file and fix requests
1 parent 8dfd1e0 commit e378f08

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

Python/Twitter_Scrapper/README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
This script extracts all Tweets, Retweets, Images, Videos, Hashtags, Likes and Pinned tweet of a specific user in a .csv file.
44

5-
![How to use the script](https://i.ibb.co/mcjK65f/Tweeter-Scrapper.png)
6-
75
### Requirements
86

97
1. The system should have **python** and **tweepy** installed. Also, the user should have a **Twitter developer account** and a **Twitter app** (if you do not have one, create one at [here](https://developer.twitter.com/)). The use of Tweepy and need for a developer account is in accordance with the Twitter scraping rules.
@@ -12,15 +10,16 @@ This script extracts all Tweets, Retweets, Images, Videos, Hashtags, Likes and P
1210
### Setup
1311

1412
1. Create a Virtual Environment.
15-
2. Install the requirements by using `pip install -r requirements.txt`
16-
3. Open the script in any text editor/IDE
17-
4. Get the credentials from the app created by you on twitter developer account and enter them here.
18-
5. Also add the path to your chrome driver in the PATH_CHROME_DRIVER constant
19-
6. Hurray.! You're ready to use the script to extracts all Tweets, Retweets, Images, Videos, Hashtags and Likes of a specific user.
13+
2. Install the requirements by using `pip3 install -r requirements.txt`
14+
3. Create a `.env` file like as `.env.example` and add the credentials from the app created by you on twitter developer account.
15+
4. Also add chrome driver path in .env file
16+
5. Hurray.! You're ready to use the script to extracts all Tweets, Retweets, Images, Videos, Hashtags and Likes of a specific user.
2017

2118
### Running a File
2219

2320
1. Run the Script `python twitter_scrapper.py`
24-
3. Enter the username you want the information from.
25-
4. Finally, you need to enter a Twitter username and password in order to extract the pinned Tweet (if it exists)
26-
5. The .csv file is downloaded to the folder where the file runs.
21+
2. Enter the username you want the information from.
22+
3. Finally, you need to enter a Twitter username and password in order to extract the pinned Tweet (if it exists)
23+
4. The `.csv` file is downloaded to the folder where the file runs.
24+
25+
![How to use the script](https://i.ibb.co/mcjK65f/Tweeter-Scrapper.png)

Python/Twitter_Scrapper/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ bs4==0.0.1
33
certifi==2020.12.5
44
chardet==4.0.0
55
idna==2.10
6+
lxml==4.6.2
67
oauthlib==3.1.0
78
PySocks==1.7.1
9+
python-decouple==3.4
810
requests==2.25.1
911
requests-oauthlib==1.3.0
1012
selenium==3.141.0
1113
six==1.15.0
1214
soupsieve==2.2
1315
tweepy==3.10.0
14-
urllib3==1.26.3
16+
urllib3==1.26.3

Python/Twitter_Scrapper/twitter_scrapper.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010

1111
import tweepy
1212
from bs4 import BeautifulSoup
13+
from decouple import config
1314
from selenium import webdriver
1415

1516
# Authenticate to Twitter
16-
CONSUMER_KEY = "<your-consumer-or-API-key-goes-here>"
17-
CONSUMER_SECRET = "<your-consumer-or-API-secret-goes-here>"
18-
ACCESS_KEY = "<your-access-key-goes-here>"
19-
ACESS_SECRET = "<your-access-secret-goes-here>"
17+
CONSUMER_KEY = config("CONSUMER_KEY")
18+
CONSUMER_SECRET = config("CONSUMER_SECRET")
19+
ACCESS_KEY = config("ACCESS_KEY")
20+
ACESS_SECRET = config("ACESS_SECRET")
21+
22+
# Authenticate to Twitter
2023
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
2124
auth.set_access_token(ACCESS_KEY, ACESS_SECRET)
2225
api = tweepy.API(auth)
2326

2427
URL = "https://twitter.com"
25-
PATH_CHROME_DRIVER = "<===============ENTER YOUR CHROME DRIVER PATH===========>"
28+
PATH_CHROME_DRIVER = config("PATH_CHROME_DRIVER")
2629
random_time_2_to_5 = random.randint(2, 5)
2730
random_time_5_to_10 = random.randint(5, 10)
2831

@@ -99,11 +102,18 @@ def extract_tweets(user_to_scrape):
99102
try:
100103
print("Extracting Tweets, Retweets, Media and Hashtags, please wait...!")
101104

102-
tweets = api.user_timeline(
103-
screen_name=user_to_scrape,
104-
# 200 is the maximum allowed count
105-
count=200,
106-
)
105+
tweets = None
106+
107+
try:
108+
tweets = api.user_timeline(
109+
screen_name=user_to_scrape,
110+
# 200 is the maximum allowed count
111+
count=200,
112+
)
113+
except tweepy.TweepError as error:
114+
print(error.args[0][0]["message"])
115+
time.sleep(2)
116+
main()
107117

108118
all_tweets = []
109119
url_tweets = []

0 commit comments

Comments
 (0)