Python3 implements Web page image download,

Source: Internet
Author: User
Tags website server

Python3 implements Web page image download,

First, we will introduce some basic knowledge about python web programming.

1. Difference Between GET and POST

1) POST is designed to store things on the web server, while GET is designed to fetch things from the server, and GET can also transmit less data to the server, the reason why Get can also transmit data is to design and tell the server what data you actually need. POST information is used as the content of the HTTP request, while GET is transmitted in the HTTP header;

2) The POST and GET methods are different in HTTP transmission. GET parameters are transmitted in the HTTP header, while Post data is transmitted in the HTTP request content;

3) When POST transmits data, it does not need to be displayed in the URL, but the GET method must be shown in the URL;

4) because the GET method is limited by the URL length, it can only transmit about 1024 bytes. The size of data transmitted by POST method can reach 2 MB.

2. Cookies

Cookies are often mentioned. What is Cookies? What is their function?

Cookies are a technology that allows the website server to store a small amount of data to the client's hard disk or memory, or to read data from the client's hard disk. Cookies are a very small text file placed on your hard disk by the Web server when you browse a website, it can record your user ID, password, browsed webpage, stay time, and other information.

When you come to this website again, the website can read Cookies and learn your related information to make corresponding actions, such as displaying your welcome slogans on the page, or you can log on directly without entering the ID or password.

Essentially, it can be viewed as your ID card. However, Cookies cannot be executed as code, nor are viruses transmitted. They are private to you and can only be read by servers that provide them.

The saved information fragments are stored in the form of "name/value" pairs (name-value pairs). A "name/value" pair is only a piece of named data.

A website can only obtain information stored on your computer. It cannot obtain information from other Cookies, or get anything else on your computer.

Most of the content in Cookies is encrypted. Therefore, generally, users only seem to have meaningless combinations of letters and numbers. Only CGI processors on the server can understand their true meanings.

Python3 basic functions of Web Page image download

  • Main functions to be implemented:
  • Parse image links on webpages
  • Checks the image link. If the image size does not meet the requirements, the image is not downloaded.
  • Add Exception Handling Mechanism
  • Automatic File Name extraction: extract file names directly from image links

Python3 reference code for downloading images through Web pages:

From urllib import requestimport threadingfrom time import sleep, ctimefrom html import parserdef downjpg (filepath, FileName = "default.jpg"): try: web = request. urlopen (filepath) print ("Access Network File" + filepath + "\ n") jpg = web. read () DstDir = "E: \ image \" print ("save File" + DstDir + FileName + "\ n") try: File = open (DstDir + FileName, "wb") File. write (jpg) File. close () return response t IOError: print ("error \ n") return response t Exception: print ("error \ n") returndef downjpgmutithread (filepathlist ): print ("A total of % d files need to be downloaded" % len (filepathlist) for file in filepathlist: print (file) print ("START multi-thread download ") task_threads = [] # Storage thread count = 1 for file in filepathlist: t = threading. thread (target = downjpg, args = (file, "mongod.jpg" % count) count = count + 1 task_threads.append (t) for task in task_threads: task. start () for task in task_threads: task. join () # Wait until all threads end print ("thread end") class parserLinks (parser. HTMLParser): filelist = [] def handle_starttag (self, tag, attrs): if tag = 'img ': for name, value in attrs: if name = 'src ': print (value) self. filelist. append (value) # print (self. get_starttag_text () def getfilelist (self): return self. filelistdef main (WebUrl): # globals flist if _ name _ = "_ main _": lparser = parserLinks () web = request. urlopen (WebUrl) # context = web. read () for context in web. readlines (): _ str = "% s" % context try: lparser. feed (_ str) failed t parser. HTMLParseError: # print ("parser error") pass web. close () imagelist = lparser. getfilelist () downjpgmutithread (imagelist) # downjpgmutithread (flist) # WebUrl = "http://www.baidu.com/" # web page link to be captured, saved to edisk WebUrl = "http://hi.baidu.com/yuyinxuezi/item/df0b59140a06be27f6625cd4" main (WebUrl) by default)

The above is the introduction of Python3 to implement Web page image download. I hope it will be helpful for your learning.

Articles you may be interested in:
  • Three methods for downloading images in batches using python
  • Python downloads the implementation code of the sister chart on the Multi-play image
  • Use Python to download Bing images (CODE)
  • Python method for obtaining web pages
  • How to download images from subscription sources using Python
  • How to download Douban Images Using Python
  • How to batch download images using Python
  • Python allows crawlers to download beautiful pictures
  • Python: Download all images from a specified URL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.