Home > OS >  How do I find something starting with something in JSON from requests python
How do I find something starting with something in JSON from requests python

Time:09-06

I want to find a URL in the JSON (request response) that starts with "https://freecash.com/auth/". I want it to only print that and not the other stuff, how can I do this?

The code for the request:

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

And here is where I'm trying to find that exact thing:

{z['body']

It's not either at the start or the end. how would I search for it? also I need it to only print the URL, not the rest of the response.

CodePudding user response:

you can use re module wichis for regex

import re

#Check if the string starts with "http://url1267" and ends with "string_end":

json_response = "The rain in Spain" x = re.search("^http://url1267.*'string_end'$", json_response)

change the string_end with the exactly in your case

CodePudding user response:

Write a helper function that will flatten and iterate through the values in the JSON object:

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
         

Then iterate through these, looking for the desired pattern:

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

EDIT

From a comment on another answer, it's clear that you're actually searching an html page that happens to be part of a JSON request. You can search this using a regex that finds e.g. freecash.com within quotation marks (" or ').

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

An alternative and perhaps cleaner solution is to use BeautifulSoup, which will properly parse the html content and look for links in the appropriate contexts:

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)

CodePudding user response:

Read your JSON file and then something like:

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)
  • Related