我有一个代码,它将excel表中的用户名作为输入发送,该输入存储在变量xy中的instagram API中,后者给出了结果。我将URL加载到JSON以在json模式中获取它。
例如,当我的变量xy包含" shawn_123" API的结果是:
{
"meta": {
"code": 200
},
"data": [
{
"username": "shawn_123",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885- 19/s150x150/11417456_1610194859266611_592197892_a.jpg",
"id": "641567093",
"full_name": "shawn ritz"
},
{
"username": "shawn_12345",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11324946_808347519273018_2073555780_a.jpg",
"id": "2074312361",
"full_name": "shawney"
}
]
}
我的代码是
for r in range(1,10):
var=r,sheet.cell(row=r,column=1).value
xy=var[1]
myopener=Myopener()
url=myopener.open('https://api.instagram.com/v1/users/search?q='+xy+'&count=1&access_token=641567093.1fb234f.a0ffbe574e844e1c818145097050cf33')
beta=json.load(url)
for item in beta['data']:
print(item['id'])
因为它从同一用户名检索两个输出。
注意:我想要一个正则表达式,它从json中搜索确切的用户名并保存该记录的user_id。
答案 0 :(得分:1)
您可以在打印ID之前检查username
是否匹配:
for item in beta['data']:
if item['username'] == xy: # here check the username from your input
print(item['id'])
或者,使用next
运算符:
user_id = next((item['id'] for item in beta['data'] if item['username'] == xy), None)
答案 1 :(得分:0)
d = {"data": [ { "username": "shawn_123", "profile_picture": "https://scontent.cdninstagram.com/t51.2885- 19/s150x150/11417456_1610194859266611_592197892_a.jpg", "id": "641567093", "full_name": "shawn ritz"}, { "username": "shawn_12345", "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11324946_808347519273018_2073555780_a.jpg", "id": "2074312361", "full_name": "shawney" }]}
#### To get list
user_name = 'something'
print [i['username'] for i in d['data'] if i['username'] == user_name]
#### To get the username
print [i['username'] for i in d['data'] if i['username'] == user_name][0]
答案 2 :(得分:0)
试试这个
"shawn_123".*?"id":\s"(\d+)"
<强>解释强>
.
:除了换行符sample之外的任何字符
*
:零次或多次sample
?
:一次或无sample
\s
:“空格字符”:空格,制表符,换行符,回车符,垂直制表符sample
( … )
:捕获小组sample
\
:逃脱一个特殊字符sample
+
:一个或多个sample
的Python:
import re
p = re.compile(ur'"shawn_123".*?"id":\s"(\d+)"', re.DOTALL)
test_str = u"{\n\"meta\": {\n\"code\": 200\n},\n\"data\": [\n {\n \"username\": \"shawn_123\",\n \"profile_picture\": \"https://scontent.cdninstagram.com/t51.2885- 19/s150x150/11417456_1610194859266611_592197892_a.jpg\",\n \"id\": \"641567093\",\n \"full_name\": \"shawn ritz\"\n},\n {\n \"username\": \"shawn_12345\",\n \"profile_picture\": \"https://scontent.cdninstagram.com/t51.2885-19/s150x150/11324946_808347519273018_2073555780_a.jpg\",\n \"id\": \"2074312361\",\n \"full_name\": \"shawney\"\n}"
m = re.findall(p, test_str)
print m
#[u'641567093']