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