一直在玩 Twitter API。事情进展顺利,直到我添加了开始和结束时间以及搜索的粒度。我相当有信心字符串值是正确的,按照 Twitter api 文档,并正确添加到参数中。但是我的参数做错了,有人可以帮助我吗?
有效的代码,IE 会产生 200 响应和推文:
keywords = 'telsa'
api, query = 'https://api.twitter.com/2', '/tweets/search/recent'
request = f'{api}{query}'
params = {'query': keywords,
'tweet.fields': 'created_at,lang', # no space before lang
'max_results': '10'}
header = {'authorization':f'Bearer {bearer_token}'}
response = requests.get(request, headers=header, params=params)
if response: # boolean True if response is 200,
print(response.response.json())
else:
print(response)
print(params)
Run Code Online (Sandbox Code Playgroud)
不起作用的代码:400 响应:添加 start_time、end_time、粒度
<snip>
header = {'authorization':f'Bearer {bearer_token}'}
**************** NEW CODE **************************
# Get datetime for now and 7 days ago, correctly formatted for …Run Code Online (Sandbox Code Playgroud) 如何将命名参数从另一个函数传递给一个函数?
在过去的 8 个小时里,我查看了包装器、argparse、Google。我错过了一些重要的事情。
def my_func(a='something', b='something_else'):
# add a and b
return something
def slave(func, args):
result = func(args)
return result
slave_work = {my_func, (a=50, b=90)}
print (slave(slave_work)
Run Code Online (Sandbox Code Playgroud)
应该输出140. 我怎样才能做到这一点?
我见过类似的但不一样的:这里.我绝对想要所有列表元素的排列,而不是组合.我是不同的,因为a,b,c的itertools排列返回abc而不是aba(soooo close).我怎样才能得到像aba一样的结果?
('a',) <-excellent
('b',) <-excellent
('c',) <-excellent
('a', 'b') <-excellent
('a', 'c') <-excellent
('b', 'a') <-excellent
('b', 'c') <-excellent
('c', 'a') <-excellent
('c', 'b') <-excellent
('a', 'b', 'c') <-- I need a,b,a
('a', 'c', 'b') <-- I need a,c,a
('b', 'a', 'c') <-- I need b,a,b... you get the idea
Run Code Online (Sandbox Code Playgroud)
哦最大排列长度(python.org itertools中的"r")等于len(列表),我不想包括'双打',例如aab或abb ......或者abba:P列表可以任何长度.
import itertools
from itertools import product
my_list = ["a","b","c"]
#print list(itertools.permutations(my_list, 1))
#print list(itertools.permutations(my_list, 2))
#print list(itertools.permutations(my_list, 3)) <-- this *ALMOST* works …Run Code Online (Sandbox Code Playgroud)