Python, BeautifulSoup - Parsing out a Tweet

喜欢而已 提交于 2019-12-25 08:20:41

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/9857627/python-beautifulsoup-parsing-out-a-tweet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!