If I want to use Python to steal emoticons

Start with two sentences of BB

Do you need to manually save the images one by one?

Life is short, I use Python

Here are two meme-filled URLs:

Dou Tu La

Doutu.com

Crawling emoticons, take out the key codes used and briefly talk about them.

After all, I’m not very familiar with Python, so I’ll just record what I use.

General idea:

  1. Send a get request through requests.get()
  2. Get the crawled web page data, and then find the emoticons in it
  3. Save the emoticon package to a local folder

Knowledge points:

1.requests library

requests.get()

Send get request

Installation

pip install requests

Introduction

import requests

requests get request with request parameters

url = 'http://www.bbsnet.com/biaoqingbao'
res = requests.get(url)

Modify encoding:

response.content.deocde(“utf-8”)

js
Copy code
html = res.content.decode(('utf-8'))

2.Usage of etree class in lxml module

Installation

pip install lxml

Introduction

from lxml import etree

Use etree.HTML() to convert html string into element object

tree = etree.HTML(html)

xpath method of element object

The Element object has an xpath method that returns a list of results and can accept bytes type data and str type data.

The full name of xpath: XML Path Language is a small query language

Is a language for finding information in XML documents

Advantages of XPath:

Information can be found in XML

Supports HTML search

Navigable via elements and attributes

According to the crawled web page data, the analysis shows that the emoticon picture is in the list with the id of post_container, and the location of the emoticon can be accurately found through xpath.

li_list = tree.xpath('//ul[@id="post_container"]/li')

3.Python “{}.format()” output formatting method

Starting with Python 2.6, a new function str.format() has been added to format strings, which enhances the string formatting function.

Example:

"{0} {1}".format("hello", "world") # Set the specified location

You can also set parameters:

print("Website name: {name}, address {url}".format(name="bilibili", url="https://www.bilibili.com/"))

4.with open() as f: usage

The open() function in Python is used to open a file and create a file object. Only related methods can call it for reading and writing.

with keyword

The file stream can be closed after the statement ends. Without the with keyword, the file will be closed by python garbage collection. Don’t use with, remember f.close()

Common usage:

with open(r'filename.txt') as f:
    for l in f:
        l = json.loads(l) #File reading operation

with open('Hello.txt', 'w') as f:
   f.write('hello world') #File writing operation

Identifier

with open('Hello.txt', 'w') as f: # is the identifier

The meaning of common identifiers

r: Open the file in read-only mode.
rb: Open a file in binary format for reading only.
r + : Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb +: Open a file in binary format for reading and writing.
w: Open a file for writing only. If the file already exists, it is overwritten. If the file does not exist, create a new file.
wb: Open a file in binary format for writing only. If the file already exists, it is overwritten. If the file does not exist, create a new file.
w + : Open a file for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file.
wb +: Open a file in binary format for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file.
a: Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, new content will be written after existing content. If the file does not exist, create a new file for writing.
ab: Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, new content will be written after existing content. If the file does not exist, create a new file for writing.
a + : Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, a new file is created for reading and writing.
ab +: Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, a new file is created for reading and writing.

Method attributes of f:

file.read([size]) #Return file data as a string. The optional parameter size controls the number of bytes read.
file.readlines([size]) #Return a list of line contents in the file, the size parameter is optional
file.write(str) #Write string to file
file.writelines(strings) #Write string sequence to file
file.close() #Close the file
file.closed #Indicates that the file has been closed, otherwise it is False

file.mode #AccessThe access mode used when opening a file
file.encoding #The encoding used by the file
file.name #file name
file.newlines #None when no line delimiter is read, a string when there is only one line delimiter, and a string containing all currently encountered lines when the file has multiple types of line terminators. end list
file.softspace #0 means adding a space character after outputting a data, 1 means not adding it. This attribute is generally not used by programmers and is used internally by the program.

640.png

Results

Run the Python file, and as you wish, the emoticon package will run into my folder.

640 (1).png

Tips

In addition to this, you can also install extensions on your Chrome browser.

Plug-in: ImageAssistant batch image download.

640 (2).png Downloading through plug-ins is much faster than Python crawling emoticons.

Life is short, I use ImageAssistant

Don’t waste your precious time learning Python

Manual dog head.

Final words

If the above is useful to you, you might as well like, collect and follow it, thank you

The rapid rise of Python is extremely beneficial to the entire industry, but “There are many popular people and not many people“, which has led to a lot of criticism, but it still cannot stop its popularity. development momentum.

If you are interested in Python and want to learn Python, here I would like to share with you a Complete set of Python learning materials, which I compiled during my own study. I hope it can help you, let’s work together!

Friends in need can click the link below to get it for free or Scan the QR code below to get it for free
Click here

1Getting started with zero basics

① Learning route

For students who have never been exposed to Python, we have prepared a detailed Learning and Growth Roadmap for you. It can be said to be the most scientific and systematic learning route. You can follow the above knowledge points to find corresponding learning resources to ensure that you learn more comprehensively.

② Route corresponding learning video

There are also many learning videos suitable for beginners. With these videos, you can easily get started with Python~

③Exercise questions

After each video lesson, there are corresponding exercises to test your learning results haha!

2Domestic and foreign Python books and documents

① Documents and books

3Python toolkit + project source code collection

①Python toolkit

The commonly used development software for learning Python is here! Each one has a detailed installation tutorial to ensure you can install it successfully!

②Python practical case

Optical theory is useless. You must learn to type code along with it and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases. 100+ practical case source codes are waiting for you!

③Python mini game source code

If you feel that the practical cases above are a bit boring, you can try writing your own mini-game in Python to add a little fun to your learning process!

4Python interview questions

After we learn Python, we can go out and find a job if we have the skills! The following interview questions are all from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. I believe everyone can find a satisfactory job after reviewing this set of interview materials.

5Python part-time channel

Moreover, after learning Python, you can also take orders and make money on major part-time platforms. I have compiled various part-time channels + part-time precautions + how to communicate with customers into documents.

All the above information , if friends need it, you can scan the QR code below to get it for free