Skip to main content
Chemistry LibreTexts

1: Requests

  • Page ID
    431870
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Requests allow you to make HTTP requests and pull data from web API  services. requests 2.31.0 was released May 22, 2023 and is available through PyPi and supports Python 3.7+,  https://pypi.org/project/requests/#description

    To install reqquests use the following BASH script

    pip install requests
    

    Now that you have installed requests, let's use the PubChem PUG-REST (Power User Gateway) API interface to programmatically access the molar mass of a chemical from its name. See the 2019 Cheminformatics OLCC module on PUG-REST to see how this works and other types of information you can obtain

    After you install requests you can run the following python script in Thonny or your preferred python IDE. Note, you can run it in the Binder box below because the LibreTexts Jupyter Hub that runs these scripts online already has requests installed, but it will take a minute to initialize the first time.

    import requests
    def getMolarMass(compound_name):
        uri = ("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/"
               +str(compound_name) +"/property/MolecularWeight/txt")
        res = requests.get(uri)
        molar_mass=float(res.text.rstrip()) 
        return molar_mass
    name=input("enter compound name: ")
    molarmass=getMolarMass(name)
    
    print(molarmass)
            hello world
          

    This page titled 1: Requests is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?