0

Having trouble figuring out a failure in my PY code, it looks like it might be a JSON failure, not sure what exactly is causing it. From my reading it is possible that the issue is because the code is grabbing information that might not be json anymore. I assume that expecting value means that no value was given, which would likely show that the first error is caused by it being unable to retrieve the information type, non-json.

I am not sure how to best figure out what it is grabbing so that I can investigate how to edit the script to grab the non-json information.

Below is the error:

Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python311\Lib\requests\models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\temp\Dell Driver Scraper Py 3-1.py", line 51, in <module>
    updates_data = requests.get(f"{dell_api}{urlencode(payload)}", headers=headers).json()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python311\Lib\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Below here is the Py Code:

#Requires installs:
#tabulate
#requests
#certifi
#charset_normalizer
#idna
#urllib3

#Make sure python directory path added to PATH environmental Variable

#Made with the help of baduker on StackOverflow

#WT64a = Windows 10
#W2021 = Windows 11

#In the case of certificate issues use this:
#import requests as r
#print(r.certs.where())
#To find the location of Python's cert store and add the cert it needs

import csv
import time
from urllib.parse import urlencode
import tkinter as tk
from tkinter import filedialog

import requests
from tabulate import tabulate

root = tk.Tk()
root.withdraw()

file_path = "C:/temp/DellDriver_productcodes.txt"

with open(file_path, 'r') as f:
   list = f.readlines()

for items in list:
   product_code = items.replace('\n','')
   dell_api = "https://www.dell.com/support/driver/en-us/ips/api/driverlist/fetchdriversbyproduct?"
   headers = {
       "referer": f"https://www.dell.com/support/home/en-us/product-support/product/{product_code}/drivers",
       "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
       "x-requested-with": "XMLHttpRequest",
   }
   if "precision" in product_code:
      lob1 = 'Dell%20Precision%20Mobile%20WorkStation'
   elif "inspiron" in product_code:
      lob1 = 'Inspiron'
   elif "xps" in product_code:
      lob1 = 'XPS'
   payload = {
       "productcode": product_code,
       "oscode": "W2021",
       "lob": lob1,
       "initialload": True,
       "_": time.time() * 1000,
   }
   updates_data = requests.get(f"{dell_api}{urlencode(payload)}", headers=headers).json()
   row_headers = [
       "DriverName", "ReleaseDate", "LUPDDate", 
       "DellVer", "Imp","CatName", 
       "Type", "BrfDesc","File Name",
       "HttpFileLocation"
   ] 
   rows = [
       [
           driver['DriverName'],
           driver['ReleaseDate'],
           driver['LUPDDate'],
           driver['DellVer'],
           driver['Imp'],
           driver['CatName'],
           driver['Type'],
           driver['BrfDesc'],
           driver['FileFrmtInfo']['FileName'],
           driver['FileFrmtInfo']['HttpFileLocation']
       ] for driver in updates_data["DriverListData"]
   ]  
   with open("C:/DellDrivers/" + product_code + ".csv", "w", newline='') as output:
       w = csv.writer(output)
       w.writerow(row_headers)
       w.writerows(rows)

Below here is an example of the product codes in the text file (C:/temp/DellDriver_productcodes.txt)

inspiron-15-5510-laptop

inspiron-15-5570-laptop

So I found from the following post python requests GET returning HTTP 204

that you can check the response code and if it gets code 204 it might mean that it is taking too long to process the page, my code output the response code 204

They show a possible fix in the post, but I can't seem to get it to work, I might be placing it in the wrong place, but not sure. It seems to be continuously getting the 204 response.

rcode = requests.get(f"{dell_api}{urlencode(payload)}")
while rcode.status_code == 204:
   time.sleep(1)
   rcode = requests.get(f"{dell_api}{urlencode(payload)}")

by the way, the api request works fine when going through dell site and looking at the developer tools. I do not see a response 204, just 200 (success)

Karbashi
  • 27
  • 5

0 Answers0