Python Requests Get not Working

12,766

Your HEADERS format is wrong. I tried with this code and it worked without any issues:

import requests
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
}
url = 'http://stats.nba.com/stats/playbyplayv2?EndPeriod=10&EndRange=55800&GameID=0021500281&RangeType=2&Season=2016-17&SeasonType=Regular+Season&StartPeriod=1&StartRange=0'
response = requests.get(url, timeout=5, headers=HEADERS)
print(response.text)
Share:
12,766

Related videos on Youtube

Nole
Author by

Nole

Updated on June 04, 2022

Comments

  • Nole
    Nole almost 2 years

    I have a simple Get request I'd like to make using Python's Request library.

    import requests
    HEADERS = {'user-agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)'
                              'AppleWebKit/537.36 (KHTML, like Gecko)'
                              'Chrome/45.0.2454.101 Safari/537.36'),
                              'referer': 'http://stats.nba.com/scores/'}
    url = 'http://stats.nba.com/stats/playbyplayv2?EndPeriod=10&EndRange=55800&GameID=0021500281&RangeType=2&Season=2016-17&SeasonType=Regular+Season&StartPeriod=1&StartRange=0'
    response = requests.get(url, timeout=5, headers=HEADERS)
    

    However, when I make the requests.get call, I get the error requests.exceptions.ReadTimeout: HTTPConnectionPool(host='stats.nba.com', port=80): Read timed out. (read timeout=5). But I am able to copy/paste that url into my browser and view the resulting JSON. Why is requests not able to get the result?

    • Paulo Scardine
      Paulo Scardine almost 6 years
      When concatenating the strings by ('AAA' 'BBB') you get AAABBB not AAA BBB so I guess you are missing a few spaces in your user-agent header.