Home>

I'm trying to find a URL in a user's profile using Tweepy. At the red circle.
I want to get it as a character string.

The problem i am having

If i try to output the URL in the User object, you will get a character string with the link omitted.

Corresponding source code
user = api.get_user ("Twitter")
print (user.description) #For confirmation
print (user.url)
Ideal output
https://about.twitter.com/ja.html
  • Answer # 1

    You can get it normally with the API.

    user = api.get_user ("Twitter")
    url = user.entities ["url"] ["urls"] [0] ["expanded_url"]
    print (url)

    Output:

    https://about.twitter.com/

  • Answer # 2

    When you actually access the shortened URL, you get the expanded URL.

    import urllib.request
    req = urllib.re
    req = urllib.request.Request ('https://t.co/TAXQpsHa5X', method ='HEAD')
    resp = urllib.request.urlopen (req)
    print (resp.url)
    # https://about.twitter.com/ja.html

    requestsYou can also use a library such as.