Created
November 10, 2014 04:47
-
-
Save pankaj28843/3a015a1405dbb358eba7 to your computer and use it in GitHub Desktop.
Extract Links for a particular Tweet, given absolute URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from lxml import etree | |
def extract_links_from_a_tweet(tweet_url): | |
response = requests.get(tweet_url) | |
if response.status_code != 200: | |
return None | |
root = etree.HTML(response.text) | |
try: | |
p = root.xpath(".//p[@class='js-tweet-text tweet-text']")[0] | |
except IndexError: | |
return None | |
links = map(lambda a: a.attrib["data-expanded-url"], p.xpath(".//a[@rel='nofollow']")) | |
return links |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment