Damien
Damien

Reputation: 611

Python, BeautifulSoup - Parsing out a Tweet

I have a peice of HTML I took from the source of my Twitter timeline, shown here:

http://pastebin.com/deefvbYw

That's one Tweet I'll use for an example. I can't for the life of me get it to co-operate. I want it to show:

Dmitri @TheFPShow "I do this all the time... youtube.com/watch?v=DF9WP8…"

If anyone could offer some suggestions that'd be great.

Upvotes: 0

Views: 1678

Answers (1)

Asterisk
Asterisk

Reputation: 3574

soup = BeautifulSoup(twit)

name_tag = soup('strong', {'class': 'fullname js-action-profile-name show-popup-with-id'})
user = name_tag[0].contents[0]

action_tag = soup('span', {'class': 'username js-action-profile-name'})
at_sign = action_tag[0].contents[0].contents[0]
show_name = action_tag[0].contents[1].contents[0]

twit_text = soup('p', {'class': 'js-tweet-text'})
message = twit_text[0].contents[0]
url = twit_text[0].contents[1]['data-expanded-url']

print user, at_sign, show_name, message, url

The output:

Dmitri @ TheFPShow I do this all the time...  http://www.youtube.com/watch?v=DF9WP87KNPk

Upvotes: 1

Related Questions