如何从请求python中找到以JSON开头的内容



我想在JSON(请求-响应(中找到一个以"开头的URL;https://freecash.com/auth/"。我想只打印那个,不打印其他东西,我怎么能这样做?

请求的代码:

z = requests.get(f"https://www.1secmail.com/api/v1/?action=readMessage&login={user}&domain={dom}&id={z[c]['id']}").json()

我正试图在这里找到确切的东西:

{z['body']

它不是在开始,也不是在结束。我该如何搜索它?我还需要它只打印URL,而不是其余的响应。

您可以为regex 使用re-module wichis

进口再

#检查字符串是否以"0"开头;http://url1267"并以";字符串结束":

json_response=";西班牙的雨;x=重新搜索("^http://url1267.*'string_end'$";,json_response(

使用更改字符串结束

读取JSON文件,然后读取以下内容:

z = requests.get(f"https://www.1secmail.com/api/v1/?action=readMessage&login={user}&domain={dom}&id={z[c]['id']}").json()
for line in z:
line = line.rstrip()
if line.startswith('http://url1267'):
print(line)

编写一个助手函数,该函数将对JSON对象中的值进行扁平化和迭代:

def get_values_from_json(obj):
if type(obj) is dict:
for item in obj.values():
yield from get_values_from_json(item)
elif type(obj) is list:
for item in obj:
yield from get_values_from_json(item)
else:
yield obj

然后迭代这些,寻找所需的模式:

for item in get_values_from_json(z["body"]):
if item.startswith("http://url1267"):  # or use regex if this is more complicated
print(item)

编辑

从对另一个答案的评论中可以清楚地看出,您实际上是在搜索一个恰好是JSON请求一部分的html页面。您可以使用正则表达式进行搜索,例如在引号("'(中查找freecash.com

import re
for i, item in enumerate(re.findall(r'["']([^"']*?freecash.com[^"']*?)["']', z["body"])):
print(i, item)

另一种可能更干净的解决方案是使用BeautifulSoup,它将正确解析html内容并在适当的上下文中查找链接:

from bs4 import BeautifulSoup
soup = BeautifulSoup(z["body"], features="lxml")
for tag in soup.find_all("a"):
link = tag.get("href")
if "freecash.com" in link:
print(link)

最新更新