Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Forum » x-Forum » Question

Scraping Twitter replies/comments using Tweepy

Hi all, hope you're well.

I'm a novice when it comes to Python (and programming languages in general), although I'm trying to educate myself as it's becoming increasingly more important in my job role. So apologies in advance for my lack of knowledge.

I'm trying to extract all comments and replies from a twitter post using Twitters API, Tweepy and Python. I've found a template script that I've adapted and am trying to use so I'll paste it below. 

import csv
import tweepy

# get credentials at developer.twitter.com
auth = tweepy.OAuthHandler('xxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
auth.set_access_token('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

api = tweepy.API(auth)

# update these for whatever tweet you want to process replies to
name = 'ChristieLinford'
tweet_id = '1279464555547168768'

for tweet in tweepy.Cursor(api.search,q='to:'+name, result_type='recent', timeout=999999).items(1000):
    if hasattr(tweet, 'in_reply_to_status_id_str'):
        if (tweet.in_reply_to_status_id_str==tweet_id):

with open('replies_clean.csv', 'wb') as f:
    csv_writer = csv.DictWriter(f, fieldnames=('user', 'text'))
    for tweet in replies:
        row = {'user': tweet.user.screen_name, 'text': tweet.text.encode('ascii', 'ignore').replace('\n', ' ')}

When running the above I receive the following error: "TypeError: a bytes-like object is required, not 'str'"

I would be very grateful if anybody could assist me with this script, and also if anyone could clarify whether this script will pull direct replies only, or replies and sub replies?

Many thanks in advance,


Posted by 10072020LINFORD 2020-07-15 10:18:05

No Ans Posted

Please login to comment

Log In

Ask New Question