I
I
Ilya T.2021-07-08 11:25:02
Python
Ilya T., 2021-07-08 11:25:02

Why curl works but python requests doesn't?

Hello.
Here is a command from the console that works exactly as I need and returns exactly what I want:

curl https://uk.finance.yahoo.com/quote/AAPL/history

and here is the python code, which, in my understanding, does about the same thing:
url = 'https://uk.finance.yahoo.com/quote/AAPL/history'
r = requests.get(url)

gets 404.
CHADNT ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-07-08
@Insaned

It looks like the site trusts the curl user agent to be curl/7.77.0, but not the default requests.

r = requests.get(url, headers={'User-Agent': 'curl/7.77.0'})

Instead curl/7.77.0, you can specify any convenient user agent, let's say from a browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question