Python uses the API to obtain the QR code of the specified link

Paste code first

from requests import get
from os import getcwd
from time import localtime
from time import sleep

def Get_ORCode(url,size=250):
    url = "https://api.qrserver.com/v1/create-qr-code/?size={1}x{1}&data={0}".format(url,size)
    res0=get(url).content
    return res0

if __name__ == '__main__':
    url=input("Please enter the web address:")
    name=str(localtime()[:-6])[1:-1].replace(",","-")
    with open(r"{0}\{1}.png".format(getcwd(),name),"wb") as file:
        file.write(Get_ORCode(url))

Still dazzling

Required modules:

        get of request         (used to make network requests)

        getcwd of os           (get the current path when saving the picture) [optional]

        localtime of time     (date the picture was saved)                    [optional]

        sleep for time           (for console delayed exit)             [optional]

Implementation principle:

        1. Use request to send network request

        2. Get the picture and save it

Step 1: import module

Step 2: access interface

         Write a function here:

def Get_ORCode(url,size=250):

        url: is the specified link

        Size: the size of the picture (not the size of the QR code), up to 250

        Then access the interface in the function

    url = "https://api.qrserver.com/v1/create-qr-code/?size={1}x{1}&data={0}".format(url,size)
    res0=get(url).content
    return res0

        This step is to pass the specified parameters to the url, make a request and read it, and then return the binary code of the image

        api parameters:

                size: the same as the parameters of the previous function, but the length and width are consistent. Even if the writing is inconsistent, the returned is also a square

                data: just put the url in

Step 3: save

        Write a program to run as a main program:

if __name__ == '__main__':

        First you have to name the picture:

    name=str(localtime()[:-6])[1:-1].replace(",","-")

        In fact, it is to obtain the current time, remove other data, leave only the year, month and day, turn it into a string, remove the beginning and end parentheses, and replace the comma with a short horizontal

.         Then open a picture file

    with open(r"{0}\{1}.png".format(getcwd(),name),"wb") as file:
        file.write(Get_ORCode(url))

        Here is to get the current path, add the just time (name), and then write the requested data

        It's done here

         If you need to display some information through the console, add this

    print("Successfully generated")
    print(getcwd()+"\\"+name+".png")
    sleep(100)

        Output successfully obtained, output picture path

        Let the thread hang for 100 seconds, that is, prevent the program from ending. Because the program ends, the console will automatically close and you can't see anything, so you should stay

okay

Paste the code of the console version

from requests import get
from os import getcwd
from time import localtime
from time import sleep

def Get_ORCode(url,size=250):
    url = "https://api.qrserver.com/v1/create-qr-code/?size={1}x{1}&data={0}".format(url,size)
    res0=get(url).content
    return res0

if __name__ == '__main__':
    url=input("Please enter the web address:")
    name=str(localtime()[:-6])[1:-1].replace(",","-")
    with open(r"{0}\{1}.png".format(getcwd(),name),"wb") as file:
        file.write(Get_ORCode(url))

    print("Successfully generated")
    print(getcwd()+"\\"+name+".png")
    sleep(100)

effect:

 

 

Keywords: Python crawler api

Added by millergroup on Sat, 02 Oct 2021 06:56:40 +0300