Python bulk download Baidu paste post pictures

Source: Internet
Author: User

Bulk Download all the pictures posted in a post, or just download a page of pictures.

#!/usr/bin/env Python3ImportReImporturllib.requestclassdowntiebaimg:def __init__(self, url): Self.url=URLdefgetimglinks (self): response=Urllib.request.urlopen (self.url) pattern= Re.compile (r'', Re. S) Imglinks= Re.findall (Pattern, Response.read (). Decode ('Utf-8'))        returnimglinksdefSaveimgs (Self, path='./', pn="'): Imglinks=self.getimglinks ()if  notPath.endswith ('/'): Path+='/'name=0 forLinkinchimglinks:Print(link) urllib.request.urlretrieve (link, path+ STR (PN) +'_%s.jpg'%name) name+ = 1Print('completed')defMain (path='./'): URL= Input ('Enter the posting URL:') Img=downtiebaimg (URL) pn= Input ('Enter the number of posts you want to save, leave the current page where the URL is located, and enter X for the previous X page: \ n')    if  notPn:Img.saveImgs (path)Else: _url= Url.split ('? pn=') Img.url=_url[0] Img.saveimgs (Path,1)         forIinchRange (2, int (PN) + 1): _url= Url.split ('=') Img.url= _url[0] +'='+Str (i) Img.saveimgs (path, i)if __name__=='__main__': Main ()

Run as follows: Download the image from the top 3 page of the URL post in the diagram. In the figure, enter 3 to download the first 3 pages, and if you leave the page blank, download page 2nd because the posting URL you entered is pn=2.

Python bulk download Baidu paste post pictures

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.