Home > Back-end >  Trying to get data from json
Trying to get data from json

Time:07-11

import requests
from bs4 import BeautifulSoup
import json
headers = {
    'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8,pt;q=0.7',
    'Connection': 'keep-alive',
    'Origin': 'https://www.nationalhardwareshow.com',
    'Referer': 'https://www.nationalhardwareshow.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'accept': 'application/json',
    'content-type': 'application/x-www-form-urlencoded',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}
base_url='https://api.reedexpo.com/v1/organisations/'

params = {
    'x-algolia-agent': 'Algolia for vanilla JavaScript 3.27.1',
    'x-algolia-application-id': 'XD0U5M6Y4R',
    'x-algolia-api-key': 'd5cd7d4ec26134ff4a34d736a7f9ad47',
}

data = '{"params":"query=&page=0&facetFilters=&optionalFilters=[]"}'

resp = requests.post('https://xd0u5m6y4r-3.algolianet.com/1/indexes/event-edition-eve-e6b1ae25-5b9f-457b-83b3-335667332366_en-us/query', params=params, headers=headers, data=data).json()
productlinks=[]
for item in resp['hits']:
    url=base_url item['organisationGuid'] "/exhibiting-organisations?eventEditionId=" item['eventEditionExternalId']
    productlinks.append(url)
    
for link in productlinks:
    title=link['_embedded']['companyName'].json()
    print(title)

I am trying to get data but they show me str must be integer these is the page link where I get json enter image description here

CodePudding user response:

To get companyName from the request you can use next example:

import requests


organisationGuid = "org-8e39ea94-5500-4798-8fe6-b8fab875dd87"
eventGuid = "eve-e6b1ae25-5b9f-457b-83b3-335667332366"

url = f"https://api.reedexpo.com/v1/organisations/{organisationGuid}/exhibiting-organisations?eventEditionId={eventGuid}"

headers = {"x-clientid": "uhQVcmxLwXAjVtVpTvoerERiZSsNz0om"}

data = requests.get(url, headers=headers).json()
print(data["_embedded"][0]["companyName"])

Prints:

21st Century Inc

CodePudding user response:

If you are specifically out for the address as posted in your question, the following code in python can reproduce the get request send by my browser:

import requests

headers = {
    'x-clientid': 'uhQVcmxLwXAjVtVpTvoerERiZSsNz0om',
    'x-correlationid': 'cc4d6e40-2285-4585-b281-4ba84761d289',
}

params = {
    'eventEditionId': 'eve-e6b1ae25-5b9f-457b-83b3-335667332366',
}

response = requests.get('https://api.reedexpo.com/v1/organisations/org-8e39ea94-5500-4798-8fe6-b8fab875dd87/exhibiting-organisations', params=params, headers=headers)

This will return a bit more info, but we can filter it for address details only:

print(response.json()["_embedded"][0]["multilingual"][0]["address"])
{'addressLine1': '2950 Fretz Valley Rd', 'addressLine2': '', 'city': 'Perkasie', 'stateProvince': 'PA', 'postcode': '18944-4034', 'countryCode': 'USA', 'country': 'United States'}
  • Related