JSON from webpage into Python script: urllib.error.HTTPError: HTTP Error 403: Forbidden

Mecra Yavçın

I made a program and it works with a local json (data) file!

Code Block:

def datas(self):        
        
        with open ("C:\\Users\\Messi\\Desktop\\Python\\\\tek.json", "r") as dosya:            
            dataApi = json.load(dosya)     
        return dataApi

I uploaded this data json to a website which is a Lemp Server! https://bestpurpleshampoo.com/tek.json

I changed the code block to the:

import urllib.request, json 
with urllib.request.urlopen("https://bestpurpleshampoo.com/tek.json") as url:
    dataApi= json.load(url)
return dataApi

There is an error:

    Traceback (most recent call last):
   File "c:\Users\Messi\Desktop\Python\Projects\Football Tips\Kivy\main.py", line 150, in <module>
     BetpredictorApp().run()
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\app.py", line 954, in run
     self._run_prepare()
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\app.py", line 923, in _run_prepare
     self.load_kv(filename=self.kv_file)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\app.py", line 696, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\lang\builder.py", line 305, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\lang\builder.py", line 403, in load_string
     widget = Factory.get(parser.root.name)(__no_builder=True)
   File "c:\Users\Messi\Desktop\Python\Projects\Football Tips\Kivy\main.py", line 16, in __init__
     self.data = self.datas()
   File "c:\Users\Messi\Desktop\Python\Projects\Football Tips\Kivy\main.py", line 134, in datas
     with urllib.request.urlopen("https://bestpurpleshampoo.com/tek.json") as url:
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\urllib\request.py", line 214, in urlopen
     return opener.open(url, data, timeout)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\urllib\request.py", line 523, in open
     response = meth(req, response)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\urllib\request.py", line 632, in http_response
     response = self.parent.error(
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\urllib\request.py", line 561, in error
     return self._call_chain(*args)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\urllib\request.py", line 494, in _call_chain
     result = func(*args)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\urllib\request.py", line 641, in http_error_default
     raise HTTPError(req.full_url, code, msg, hdrs, fp)
 urllib.error.HTTPError: HTTP Error 403: Forbidden

Also: https://www.pythonpool.com/urllib-error-httperror-http-error-403-forbidden/

from urllib import request
from urllib.request import Request, urlopen     
url = "https://bestpurpleshampoo.com/tek.json"
request_site = Request(url, headers={"User-Agent": "Mozilla/5.0"})
webpage = urlopen(request_site).read()
print(webpage[:500])

In above solution i got:

Traceback (most recent call last):
   File "c:\Users\Messi\Desktop\Python\Projects\Football Tips\Kivy\main.py", line 151, in <module>
     BetpredictorApp().run()
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\app.py", line 954, in run
     self._run_prepare()
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\app.py", line 923, in _run_prepare
     self.load_kv(filename=self.kv_file)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\app.py", line 696, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\lang\builder.py", line 305, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Users\Messi\anaconda3\envs\kivyEnv\lib\site-packages\kivy\lang\builder.py", line 403, in load_string
     widget = Factory.get(parser.root.name)(__no_builder=True)
   File "c:\Users\Messi\Desktop\Python\Projects\Football Tips\Kivy\main.py", line 16, in __init__
     self.Today = self.todayMatches()
   File "c:\Users\Messi\Desktop\Python\Projects\Football Tips\Kivy\main.py", line 124, in todayMatches
     for i in range(len(self.data['today']["home"])):
 TypeError: byte indices must be integers or slices, not str

How possible is it that this json is works perfect locally! Team names are STR and stats are also integer! But with above method that i get error

I tried some other methods in but not tried all! How to get JSON from webpage into Python script

Could you please help me to fix this :) I have 500 hours on Python that i like it but stucked here

Thanks

user56700

I'm not 100% sure I understand what the problem is, but if I use this code it works fine from my system:

import requests
url = "https://bestpurpleshampoo.com/tek.json"
response = requests.get(url).json()
print(response['today']['home'])

Result:

['Ajax', 'Liverpool', 'Club Brugge', 'FC Porto'... truncated ...]

If you need to install requests library, use: pip install requests

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Beautiful Soup - urllib.error.HTTPError: HTTP Error 403: Forbidden

urllib.error.HTTPError: HTTP Error 403: Forbidden for urlretrieve

urllib2.HTTPError: HTTP Error 403: Forbidden

urllib2.HTTPError: HTTP Error 403: Forbidden

Python 3.6.2 url.request.urlopen() urllib.error.HTTPError: HTTP Error 403: Forbidden

urllib.error.HTTPError: HTTP Error 403: Forbidden with urllib.requests

urllib.error.HTTPError: HTTP Error 403: Forbidden in my web scrapping

HTTPError: HTTP Error 403: Forbidden on Google Colab

torchvision MNIST HTTPError: HTTP Error 403: Forbidden

raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden

download image from url using python urllib but receiving HTTP Error 403: Forbidden

bokeh sample data download fail with 'HTTPError: HTTP Error 403: Forbidden'

Web Scraping getting error (HTTP Error 403: Forbidden) using urllib

gTTS HTTPError: 403 Client Error: Forbidden for url

HTTP Error 403: Forbidden while downloading file using urllib

urllib.error.HTTPError: HTTP Error 503: Service Unavailable python

Python: urllib.error.HTTPError: HTTP Error 404: Not Found

Python urllib.error.HTTPError: HTTP Error 404: Not Found

urllib.error.HTTPError: HTTP Error 502: Bad Gateway PYTHON

urllib.error.HTTPError: HTTP Error 404: Not Found Python while scraping data from Metacritic

python urllib.error.HTTPError

Python 3, urlopen - HTTP Error 403: Forbidden

Python 3.5 urllib.request 403 Forbidden Error

Python: urllib2.HTTPError: HTTP Error 300: Multiple Choices

urllib2.HTTPError: HTTP Error 400: Bad Request - Python

urllib2 Error 403: Forbidden

HTTP Error 403: Forbidden with urlretrieve

Python: urllib.error.HTTPError: HTTP Error 525: Origin SSL Handshake Error

Pytube error: urllib.error.HTTPError: HTTP Error 404: Not Found

TOP Ranking

  1. 1

    How can I create a column whose entries depend on the contents of a column in another table?

  2. 2

    pump.io port in URL

  3. 3

    How to display items using sessions

  4. 4

    BigQuery - concatenate ignoring NULL

  5. 5

    no applicable method for 'mutate_' applied to an object of class "c('integer', 'numeric')"

  6. 6

    How i extract text from a model dialog in selenium?

  7. 7

    x no applicable method for 'tbl_vars' applied to an object of class "c('double', 'numeric')"

  8. 8

    AirflowException: Celery command failed - The recorded hostname does not match this instance's hostname

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    Most efficient regex for checking if a string contains at least 3 alphanumeric characters

  11. 11

    using excel range find return Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

  12. 12

    redirect your computer port to url

  13. 13

    Can't create database with Symfony 4.1 : could not find driver

  14. 14

    How to fix "Exception evaluating SpringEL expression" error after submitting a variable Spring/Thymeleaf

  15. 15

    A python function to get the maximum value of a specific column, given a string that represents a table in CSV format

  16. 16

    Double spacing in rmarkdown pdf

  17. 17

    Uploading Files on production server returns either CORS error or POST 400 Bad Request using Apollo-Graphql

  18. 18

    Android Kotlin Unit test failing with io.mockk.MockKException: no answer found for

  19. 19

    Change Model input_shape but got an : ValueError: Input 0 of layer dense_44 is incompatible with the layer

  20. 20

    Formik, Yup - How to check is Decimal number

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive