Python Forum
need help writing a program to run VIN numbers, any advice?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help writing a program to run VIN numbers, any advice?
#1
Hi all, I have been on the search for about half a year now for a truck my father sold in 2017 after all else has failed I have decided to start running each serial number for the VIN of the truck model, for those who don't know a VIN is the code from the factory that describes the make/model/year/package, etc of your car and at the end is a serial number normally starting at 100001, using a decoding sheet I have built the vin that matches my fathers truck all that's left is to run each serial number however as you could guess that's not very feesable for a human, and that is why I am reaching out to you all, I know nothing about coding but here is what I need to build

a program that will take this VIN "1GTHR33N7JJ" and run it from "1GTHR33N7JJ100001" to "1GTHR33N7JJ600001" and log any results that come up in between, a screenshot sent to a folder is preferred however I don't know what is possible so If it just saves results to a text file then that will suffice, I would like to be able to do this myself but if all else fails I will post to the jobs forum in the hopes someone more intelligent than me can help

the website I'm using to run the VINs: https://vehicle.report/free-vin-check/
the text box when you open the link is where the VINs are entered, feel free to use the VIN:1GTHR33N7JJ100001 as an example of no result and
VIN:1GTHR33N2JJ504627 as an example of getting a result

all help is beyond appreciated this truck meant the world to my father and me as well, it was one of the few things that tied us to my dearly departed grandfather and overall was just a very cool vehicle, I look forward to your responses and hope for the best.

Best regards,
John Hinson
Reply
#2
import requests
from bs4 import BeautifulSoup


HEADERS = {"User-Agent": "from https://python-forum.io/thread-42632.html"}


def get_report(vin: str) -> str | None:
    query_url = "https://vehicle.report/wp-admin/admin-ajax.php"
    query = {"action": "vin_request", "vin": vin}

    url = query_url + "?" + "&".join(f"{item[0]}={item[1]}" for item in query.items())
    
    # on the page it is a post request were made
    # but a get request seems also to work
    response = requests.get(url, headers=HEADERS)
    
    match response.json():
        case {"status": "ok", "redirect": report}:
            return report
        case _ as msg:
            print(msg)


def download_report(report_url: str) -> list[str]:
    attrs = {"class": "highlight-list-item-description text-strong"}
    element = "span"
    response = requests.get(report_url, headers=HEADERS)
    doc = BeautifulSoup(response.content, "html.parser")

    return [
        e.text for e
        in doc.find_all(element, attrs=attrs)
    ]


vins = ("1GTHR33N2JJ504627", "1GTHR33N7JJ100001")

for vin in vins:
    if report := get_report(vin):
        print(f"Found report for '{vin}'. Downloading report")
        row = download_report(report)
        print(row)
        print()
    else:
        print(f"Found no report for '{vin}'.")
Output:
Output:
Found report for '1GTHR33N2JJ504627'. Downloading report ['2024-08-21', '1GTHR33N2JJ504627', '1988 GMC R Conventional', '36', 'United States', 'No data'] {'status': 400, 'error': 'no_records'} Found no report for '1GTHR33N7JJ100001'.
Gribouillis and Larz60+ like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Aug-21-2024, 08:29 AM)DeaD_EyE Wrote:
import requests
from bs4 import BeautifulSoup


HEADERS = {"User-Agent": "from https://python-forum.io/thread-42632.html"}


def get_report(vin: str) -> str | None:
    query_url = "https://vehicle.report/wp-admin/admin-ajax.php"
    query = {"action": "vin_request", "vin": vin}

    url = query_url + "?" + "&".join(f"{item[0]}={item[1]}" for item in query.items())
    
    # on the page it is a post request were made
    # but a get request seems also to work
    response = requests.get(url, headers=HEADERS)
    
    match response.json():
        case {"status": "ok", "redirect": report}:
            return report
        case _ as msg:
            print(msg)


def download_report(report_url: str) -> list[str]:
    attrs = {"class": "highlight-list-item-description text-strong"}
    element = "span"
    response = requests.get(report_url, headers=HEADERS)
    doc = BeautifulSoup(response.content, "html.parser")

    return [
        e.text for e
        in doc.find_all(element, attrs=attrs)
    ]


vins = ("1GTHR33N2JJ504627", "1GTHR33N7JJ100001")

for vin in vins:
    if report := get_report(vin):
        print(f"Found report for '{vin}'. Downloading report")
        row = download_report(report)
        print(row)
        print()
    else:
        print(f"Found no report for '{vin}'.")
Output:
Output:
Found report for '1GTHR33N2JJ504627'. Downloading report ['2024-08-21', '1GTHR33N2JJ504627', '1988 GMC R Conventional', '36', 'United States', 'No data'] {'status': 400, 'error': 'no_records'} Found no report for '1GTHR33N7JJ100001'.

a million thanks to you this is an invaluable resource and I cant express how thankful I am to you the only question I have is how I need to setup the VINs to run through this program because I need to go from 1GTHR33N7JJ100001 to 1GTHR33N7JJ600001 I need to go from serial number 100001 to 600001, how do I do that range do I need to make a .txt file with every single vin in that range or is there some other way?

best regards,
John Hinson
Reply
#4
(Aug-21-2024, 08:29 AM)DeaD_EyE Wrote:
import requests
from bs4 import BeautifulSoup


HEADERS = {"User-Agent": "from https://python-forum.io/thread-42632.html"}


def get_report(vin: str) -> str | None:
    query_url = "https://vehicle.report/wp-admin/admin-ajax.php"
    query = {"action": "vin_request", "vin": vin}

    url = query_url + "?" + "&".join(f"{item[0]}={item[1]}" for item in query.items())
    
    # on the page it is a post request were made
    # but a get request seems also to work
    response = requests.get(url, headers=HEADERS)
    
    match response.json():
        case {"status": "ok", "redirect": report}:
            return report
        case _ as msg:
            print(msg)


def download_report(report_url: str) -> list[str]:
    attrs = {"class": "highlight-list-item-description text-strong"}
    element = "span"
    response = requests.get(report_url, headers=HEADERS)
    doc = BeautifulSoup(response.content, "html.parser")

    return [
        e.text for e
        in doc.find_all(element, attrs=attrs)
    ]


vins = ("1GTHR33N2JJ504627", "1GTHR33N7JJ100001")

for vin in vins:
    if report := get_report(vin):
        print(f"Found report for '{vin}'. Downloading report")
        row = download_report(report)
        print(row)
        print()
    else:
        print(f"Found no report for '{vin}'.")
Output:
Output:
Found report for '1GTHR33N2JJ504627'. Downloading report ['2024-08-21', '1GTHR33N2JJ504627', '1988 GMC R Conventional', '36', 'United States', 'No data'] {'status': 400, 'error': 'no_records'} Found no report for '1GTHR33N7JJ100001'.

I just tested it and it says there's an error in line one am I missing an extra application?

Traceback (most recent call last):
File "C:\Users\khins\PycharmProjects\VIN\pythonProject\VIN.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'

Process finished with exit code 1
Reply
#5
Quote:ModuleNotFoundError: No module named 'requests'

You need to install this dependency via pip in a virtualenv and use this venv with PyCharm.

A modified version with a vin generator:
import csv
import time
from pathlib import Path

import requests
from bs4 import BeautifulSoup

HEADERS = {"User-Agent": "from https://python-forum.io/thread-42632.html"}


def get_report(vin: str) -> str | None:
    query_url = "https://vehicle.report/wp-admin/admin-ajax.php"
    query = {"action": "vin_request", "vin": vin}

    url = query_url + "?" + "&".join(f"{item[0]}={item[1]}" for item in query.items())

    # on the page it is a post request were made
    # but a get request seems also to work
    response = requests.get(url, headers=HEADERS)

    match response.json():
        case {"status": "ok", "redirect": report}:
            return report
        case _ as msg:
            pass


def download_report(report_url: str) -> list[str]:
    attrs = {"class": "highlight-list-item-description text-strong"}
    element = "span"
    response = requests.get(report_url, headers=HEADERS)
    doc = BeautifulSoup(response.content, "html.parser")

    return [e.text for e in doc.find_all(element, attrs=attrs)]


def vin_generator(prefix, start, end):
    for value in range(start, end):
        yield f"{prefix}{value}"


VIN_CSV = Path.home().joinpath("Desktop", "vin.csv")
VIN_PREFIX = "1GTHR33N2JJ"
VIN_START = 504627
VIN_END = 504630


with VIN_CSV.open("a") as vin_fd:
    writer = csv.writer(vin_fd)
    for vin in vin_generator(prefix=VIN_PREFIX, start=VIN_START, end=VIN_END):
        time.sleep(1)
        if report := get_report(vin):
            print(f"Found report for '{vin}'. Downloading report")
            row = download_report(report)
            print(row)
            writer.writerow([vin] + row)
            print()
        else:
            print(f"Found no report for '{vin}'.")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing on an lcd display gives problems to the program Idontknowany 1 1,907 Nov-15-2021, 10:46 PM
Last Post: Larz60+
  I am writing a car rental program on python. In this function I am trying to modify aboo 2 3,784 Aug-05-2021, 06:00 PM
Last Post: deanhystad
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 5,052 May-09-2019, 12:19 PM
Last Post: Pleiades
  Writing a code with 3 digit numbers while they cant contain 0 and then sum them AFKManager 2 3,073 Jan-13-2019, 12:00 PM
Last Post: perfringo
  writing numbers to csv file SchroedingersLion 7 5,524 Dec-20-2018, 10:48 AM
Last Post: perfringo
  program flow advice sampazzer 2 3,822 Aug-05-2017, 09:34 PM
Last Post: sampazzer
  Program writing excel rfile with randomnized input wepkes 1 4,474 Dec-08-2016, 08:06 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020