我正在研究我们网站的分析数据,发现有一批 t.co 的 URL 是我们推广活动的来源。我尝试着找到一种方法,通过 Twitter API 或其他途径将这些 URL 还原回它们所来源的原始推文。然而我似乎无法找到一个好的方式来实现,是否有任何方法可以做到这一点呢?
我正在研究我们网站的分析数据,发现有一批 t.co 的 URL 是我们推广活动的来源。我尝试着找到一种方法,通过 Twitter API 或其他途径将这些 URL 还原回它们所来源的原始推文。然而我似乎无法找到一个好的方式来实现,是否有任何方法可以做到这一点呢?
#!/usr/bin/env python
import requests
shorturl = raw_input("Enter the shortened URL in its entirety: ")
r = requests.get(shorturl)
print("""
The shortened URL forwards to:
%s
""" % r.url)
http://docs.python-requests.org/en/latest/index.html
重定向和历史记录部分涵盖此示例。
我不知道通过Twitter API如何实现,如果所有URL缩短都是自动的,则可能无法实现。尽管如此,基于API的解决方案仅适用于t.co地址,而上面的代码将适用于任何其他缩短的URL或任何重定向到其他位置(例如HTTP 301或302响应代码)的URL。
编辑(比从未更好一点):在使用上述方法找到t.co转发实际指向的位置后,将有三种或四种可能的结果。 最常见的是它是OP认为他们所有人都是的缩短为贴在推文中的URL,公平地说,大多数情况确实如此。
其他可能性是它链接回推文本身,这通常仅出现在某些相当长的推文中(不确定随着字符限制增加而增加多少次);以及转发到与推特作者的状态URL无关的状态的URL,这通常是嵌入式媒体(图像和视频)的情况;以及转发到正在引用推文或转推的推文的URL。
鉴于原始情景,内部Twitter用法中的任何一种都不应该被看到,只有“正常”的转发才是关注的重点。现在,在twitter.com上搜索t.co地址将无济于事,无论使用什么组合。#!/usr/bin/env python3
import requests
import urllib.parse
shorturl = input("Enter the shortened URL in its entirety: ")
r0 = requests.get(shorturl, verify=True)
t0 = "https://twitter.com/search?f=tweets&q="
t1 = urllib.parse.quote_plus(r0.url)
r1 = requests.get("{0}{1}".format(t0, t1), verify=True)
# the results will be in r1.content
# there may be some benefit from cutting the http:// or
# https:// from r0.url before creating the quoted string in t1.
然而,如果不向Twitter支付增强数据访问的费用,那就只能到这种程度了。
from requests_oauthlib import OAuth1Session
sess = OAuth1Session(
client_key=TWITTER_CONSUMER_KEY,
client_secret=TWITTER_CONSUMER_SECRET,
resource_owner_key=TWITTER_ACCESS_TOKEN,
resource_owner_secret=TWITTER_ACCESS_TOKEN_SECRET
)
def find_tweets_using_tco(tco_url):
"""
Given a shortened t.co URL, return a set of URLs for tweets that use this URL.
"""
# See https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
resp = sess.get(
"https://api.twitter.com/1.1/search/tweets.json",
params={
"q": tco_url,
"count": 100,
"include_entities": True
}
)
statuses = resp.json()["statuses"]
tweet_urls = set()
for status in statuses:
# A retweet shows up as a new status in the Twitter API, but we're only
# interested in the original tweet. If this is a retweet, look through
# to the original.
try:
tweet = status["retweeted_status"]
except KeyError:
tweet = status
# If this tweet shows up in the search results for a reason other than
# "it has this t.co URL as a short link", it's not interesting.
if not any(u["url"] == tco_url for u in tweet["entities"]["urls"]):
continue
url = "https://twitter.com/%s/status/%s" % (
tweet["user"]["screen_name"], tweet["id_str"]
)
tweet_urls.add(url)
return tweet_urls
如果你特别想从Twitter上查找引用,请这样做:site:twitter.com "https://example.com"
t.co
URL缩短服务只是在HTTP响应中重定向到另一个URL。要找到该URL,您只需要获取t.co URL并查看响应中的location
头信息。可以使用curl
实现此操作:curl -v <t.co URL>
要从所有信息中提取URL,您可以使用以下代码:
curl -w "%{redirect_url}" <t.co URL>
-w选项告诉curl
仅输出redirect_url
变量。
这是如何找到原始推文的方法:
r = requests.get(fwdurl, verify=True)
,这对HTTP和HTTPS都适用。此外,成为唯一的答案并获得赏金使某人的负评变得可笑。 - Ben