0
votes

Problem: My selenium scrapping script will not get me tweet ids, probably because of an issue with how I request them.

Details:

Hello everyone, I have a script found here (https://github.com/bpb27/twitter_scraping/blob/master/scrape.py) which goes into a twitter search and gets the ids of old tweets.

From this script I have changed two parts:

user = 'realdonaldtrump'
start = datetime.datetime(2010, 1, 1)  # year, month, day
end = datetime.datetime(2016, 12, 7)  # year, month, day

has become

user = 'metoo'
start = datetime.datetime(2017, 10, 24)  # year, month, day
end = datetime.datetime(2017, 10, 25)  # year, month, day

and

def form_url(since, until):
    p1 = 'https://twitter.com/search?f=tweets&vertical=default&q=from%3A'
    p2 =  user + '%20since%3A' + since + '%20until%3A' + until + 'include%3Aretweets&src=typd'
    return p1 + p2

has become

def form_url(since, until):
    p1 = 'https://twitter.com/search?l=fr&q=%23'
    p2 =  user + '%20since%3A' + since + '%20until%3A' + until + 'include%3Aretweets&src=typd'
    return p1 + p2

After my changes the script correctly goes to search and iterates over all tweets but does not grab any ids.

Here is the request part:

for tweet in found_tweets:
    try:
        id = tweet.find_element_by_name(id_selector).get_attribute('href').split('/')[-1]
        ids.append(id)
    except StaleElementReferenceException as e:
        print('lost element reference', tweet)

Any ideas how to fix this?

1

1 Answers

0
votes

It can be related to updates on html. Your code tweet.find_element_by_name(id_selector) should return

<a href="/xxx/status/928601524380536xxx" class="tweet-timestamp js-permalink js-nav js-tooltip" title="04:33 - 9 nov. 2017" data-conversation-id="928601524380536xxx"><span class="_timestamp js-short-timestamp " data-aria-label-part="last" data-time="xxx" data-time-ms="xxx" data-long-form="true">9 nov.</span></a>

As you can see there is no href attribute. Instead of getting href, you should get "data-conversation-id" attribute for each tweet.

id = tweet.find_element_by_name(id_selector)['data-conversation-id']

On the other hand, I would prefer not to use this id_selector ('.time a.tweet-timestamp') because tweet['data-item-id'] should give the id.

 <li class="js-stream-item stream-item stream-item" data-item-id="928601524380536xxx" id="stream-item-tweet-928601524380536xxx" data-item-type="tweet">

Bests,