How to use python urllib library

 

The urllib library is a third-party library for Python, which provides rich functions and powerful performance. The Python version is 2.11, offering options to run on different platforms, including applications for desktop and mobile devices. Unlike libraries such as pycharm and libc, urllib is not designed to build desktop applications, but it can be used for applications on mobile devices. The urllib library implements mobile device applications by being used on mobile devices without being installed in a desktop environment. In order to implement desktop applications, urllib provides several useful functions, which can be used for the following operations: 1. Select URL 2. Copy URL 3. Modify URL 4. Clear links in url 5. Close url 6. Replace old url with new url url 7. Display web page content

  • 1. Select the URL

    The first function of urllib is to select a URL, which is a very simple function that we can use to implement a desktop application. Here is a simple sample code: To use this function, please enter the following statement in the text box: Please note that urllib only supports Python version 2.11 and above, so to use this function on mobile devices, you must use the latest Version. Here's a sample code that shows how to use this function: You might wonder why URL was chosen over something else. First, we need to make sure each URL has a correct name. In the sample code below, we'll specify a new URL to display our web page: There are two URLs to choose from. First, we'll display a textbox on the screen. Enter the URL in that text box, and set it to <title> If the user enters an incorrect URL, an alert box will be displayed. If you don't want to use any error messages, you can use the following code: This function will return a URL that is the same as the current url (note: this is not the URL we want to use). So when the user enters a wrong url, an alert box will appear. The URL returned by this function is the URL you want to use, but it may be a string (eg "journal") or a list (eg "text"). In this case, we will use some of the most commonly used URLs to create the application. You can also choose other types of URLs to create desktop applications: Note that choosing a different type of URL in this example may result in errors or warnings. So make sure to check all types of URLs when you enter a new one.

  • 2. Copy the URL

    If you want to copy a URL into a new file, you must add the URL to urllib. In urllib, the URL is copied by using the following function: Then, in the code above, we use urllib's import function to add the url to the new file. This will add to a file and load it into the terminal. To do this properly, we need to use the following function in our code: If you want to copy a URL and add it to a new file, you can use the following function: This is done to make the URL consistent with the rest of the application be consistent. For example, if we want to copy a web page with the same name, we can use the following function: where, result is the URL to be copied; length is the length of the file to be added to the URL. Additionally, this function is used to add comments to URLs. This function returns a reference to the URL. Finally, we need to convert it into Python code. In the above code, we are adding the link to the url to display the web page content. To make the content of a web page easier to read, we can use the following function: This function replaces a link added to a website with a new piece of code.

  • 3. Modify the URL

    If you want a longer URL, you can use the text function from the urllib library. This function will convert your URL to a string, which is what you want to modify. There is a text function in the urllib library that can handle URLs of arbitrary length without specifying the length of the string to change. The text function is called using the following code: For example, if you want to change the length of a byte in the URL, and the string length is 50 characters, then you can use the following code to modify it: Similarly, if you want To display webpage content on mobile devices, you can use the above code to modify the content in the url.

    • 1. Modify the length of the url

      Another very useful feature of the urllib library is the ability to modify the length of your url by converting it to a string. For example, if you want to delete a paragraph in a URL, then you can do this: The above example converts the string "'_name'" to "_name_list". Because in python, the string length is represented by the number of characters and the percentage of the number of characters, so this paragraph becomes a string, so you can easily modify it through the list function in Python. In this example, we used 20 characters to modify the url.

    • 2. Display web content on mobile devices

      For web pages on mobile devices, you can use the text function in the urllib library to display web page content. We can use the following code to achieve this function: For example, we want to display a web page content about stocks on a mobile device, but we don't want to display the specific location of this web page, but want to display the content of the web page on the screen A stock name for . At this time, we can use the text function in the urllib library to call the following code: In this way, we can display a stock name on the screen, and this stock name corresponds to a stock name on the page. In addition, if you want to use other functions in the urllib library, you can find the relevant function names in the function list and use them to complete the functions you want to complete.

  • 4. Clear link

    When you visit a website using a mobile device, you may come across links, such as attachments sent by email or websites that open in your browser. These links may redirect you to unwanted websites, or may display incorrect content and prevent you from accessing the website. Clearing these links can improve the user experience of your mobile application. All links in the URL can be removed with the recursive function. For example, if you have a webpage open on a mobile device, and that webpage contains a link that contains an image, you can remove the link via the recursive function.

  • Here are a few commonly used Python urllib library code samples:

  • 1. Send a GET request and get the response:
    ```python
    import urllib.request
    url = 'http://www.example.com'
    response = urllib.request.urlopen(url)
    html = response.read().decode ('utf-8')
    print(html)
    ```
    2. Send a POST request and get the response:
    ```python
    import urllib.request
    import urllib.parse
    url = 'http://www.example.com'
    data = {'name': 'John', 'age': 25}
    data = urllib.parse.urlencode(data).encode('utf-8')
    req = urllib.request.Request(url, data)
    response = urllib .request.urlopen(req)
    html = response.read().decode('utf-8')
    print(html)
    ```
    3.Set request header information:
    ```python
    import urllib.request
    url = 'http://www.example.com'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
    req = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(req)
    html = response.read().decode('utf-8')
    print(html)
    ```
    4. 下载文件:
    ```python
    import urllib.request
    url = 'http://www.example.com/file.zip'
    filename = 'file.zip'
    urllib.request.urlretrieve(url, filename)
    ```
    5. 使用代理发送请求:
    ```python
    import urllib.request
    url = 'http://www.example.com'
    proxy_handler = urllib.request.ProxyHandler({'http': 'http://127.0.0.1:8080'})
    opener = urllib.request.build_opener(proxy_handler)
    urllib.request.install_opener(opener)
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')
    print(html)
    ```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130025626