0

I am trying to get the data from this website, https://en.macromicro.me/charts/947/commodity-ccfi-scfi ,

I understand that the data is called from an API, how do I find out how the call is made and how do I extract it using python?

I am new to python and html in general so I have no idea where to start.

I tried,

import requests
from bs4 import BeautifulSoup
import json
import pandas as pd
urlheader = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest",
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9",
    "Authorization": "Bearer 640eabc473294fbac27930ef08d28ab4",
    "Connection": "keep-alive",
    "Cookie": "PHPSESSID=5q99iuiarvf1ba2lafh6je5hr5; _ga=GA1.2.628840989.1624431403; _gid=GA1.2.146418174.1624431403; _fbp=fb.1.1624431403269.1337227854; _hjTLDTest=1; _hjid=89fd1c1b-93a7-4da9-bf90-46efcb6aae15; _hjFirstSeen=1",
    "DNT": "1",
    "Host": "en.macromicro.me",
    "Referer": "https://en.macromicro.me/charts/947/commodity-ccfi-scfi",
}

url = "https://en.macromicro.me/charts/data/947"



req = requests.post(url,headers=urlheader)
soup = BeautifulSoup(req.content, "lxml")
print(soup)

But I get the following error

<html><body><p>{"status":"Method Not Allowed","code":405,"text":"HTTP 405 (POST \/charts\/data\/947)","level":0}</p></body></html>

1 Answer 1

1

You can make a GET request to the API using requests and convert the response to json.

resp has the data in json format and you can easily extract the info that you need.

import requests

url = "https://en.macromicro.me/charts/data/947"

resp = requests.get(url)
resp = resp.json()

Sign up to request clarification or add additional context in comments.

5 Comments

hey it doesnt work, i get an error, i updated my question
your code gives this error {'status': 'Method Not Allowed', 'code': 405, 'text': 'HTTP 405 (POST /charts/data/947)', 'level': 0}
Why are you making a POST request ? My code and your code are completely different. How would you expect it to work ?
hey @Ram I dont get it, sometimes i get this {'success': 0, 'data': [], 'msg': 'error #244'} sometimes it works, sometimes i get <bound method Response.json of <Response [200]>>
is the website blocking me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.