-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommentScraper.py
More file actions
145 lines (124 loc) · 5.52 KB
/
commentScraper.py
File metadata and controls
145 lines (124 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import requests
from bs4 import BeautifulSoup
import praw
import getpass
import datetime
from pathlib import Path
import configparser
import os
import time
from typing import List, Optional
import logging
from tqdm import tqdm
class RedditScraper:
def __init__(self, config_path: str = "config.ini"):
"""Initialize the Reddit scraper with configuration."""
self.config_path = Path(config_path)
self.setup_logging()
self.config = self.load_config()
self.reddit = self.authenticate()
def setup_logging(self):
"""Set up logging configuration."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('scraper.log'),
logging.StreamHandler()
]
)
self.logger = logging.getLogger(__name__)
def load_config(self) -> configparser.ConfigParser:
"""Load or create configuration file."""
config = configparser.ConfigParser()
if not self.config_path.exists():
config["REDDIT"] = {
"client_id": input("Enter client ID: "),
"client_secret": input("Enter client secret: "),
"username": input("Enter Reddit username: "),
"password": getpass.getpass("Enter Reddit password: "),
}
with open(self.config_path, "w") as f:
config.write(f)
self.logger.info(f"Created new config file at {self.config_path}")
else:
config.read(self.config_path)
self.logger.info("Loaded existing config file")
return config
def authenticate(self) -> praw.Reddit:
"""Authenticate with Reddit API."""
try:
reddit = praw.Reddit(
client_id=self.config["REDDIT"]["client_id"],
client_secret=self.config["REDDIT"]["client_secret"],
username=self.config["REDDIT"]["username"],
password=self.config["REDDIT"]["password"],
user_agent="Comment Scraper v1.0"
)
self.logger.info("Successfully authenticated with Reddit")
return reddit
except Exception as e:
self.logger.error(f"Authentication failed: {str(e)}")
raise
def format_comment(self, comment: praw.models.Comment) -> str:
"""Format a single comment with metadata."""
return (
f"Comment ID: {comment.id}\n"
f"Content: {comment.body}\n"
f"Subreddit: {comment.subreddit_name_prefixed}\n"
f"Date: {datetime.datetime.fromtimestamp(comment.created_utc)}\n"
f"Score: {comment.score}\n"
f"{'-'*80}\n"
)
def scrape_comments(self, username: str, limit: Optional[int] = None) -> List[str]:
"""Scrape comments for a given username with error handling and rate limiting."""
comments = []
try:
redditor = self.reddit.redditor(username)
comment_iterator = redditor.comments.new(limit=limit)
# Get total number of comments for progress bar if no limit is set
if limit is None:
try:
limit = sum(1 for _ in redditor.comments.new(limit=None))
except Exception:
limit = 100 # fallback value
with tqdm(total=limit, desc="Scraping comments") as pbar:
for comment in comment_iterator:
try:
formatted_comment = self.format_comment(comment)
comments.append(formatted_comment)
pbar.update(1)
# Rate limiting
time.sleep(0.5) # Respect Reddit's rate limits
except Exception as e:
self.logger.warning(f"Error processing comment: {str(e)}")
continue
except Exception as e:
self.logger.error(f"Error scraping comments: {str(e)}")
return comments
def save_comments(self, comments: List[str], username: str) -> Path:
"""Save comments to a file with error handling."""
if not comments:
self.logger.warning("No comments found to save")
return None
output_dir = Path("scraped_comments")
output_dir.mkdir(exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = output_dir / f"{username}_comments_{timestamp}.txt"
try:
with open(filename, "w", encoding="utf-8") as f:
f.write("\n".join(comments))
self.logger.info(f"Saved {len(comments)} comments to {filename}")
return filename
except Exception as e:
self.logger.error(f"Error saving comments: {str(e)}")
return None
def main():
scraper = RedditScraper()
username = input("Enter the Reddit username to scrape: ")
limit = input("Enter number of comments to scrape (press Enter for all): ")
limit = int(limit) if limit.strip() else None
comments = scraper.scrape_comments(username, limit)
scraper.save_comments(comments, username)
if __name__ == "__main__":
main()