Python Reptile Tanabata Benefits

Source: Internet
Author: User
Tags xpath

I wish you a happy Tanabata

Sister figure

1 ImportRequests2  fromlxmlImportetree3 ImportOS4 defHeaders (referer):#The download of the picture may be related to the referer of the head, so the referer is set to a transform value to evade the anti-grilled5headers = {6         'user-agent':'mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/68.0.3440.106 safari/537.36',7         'Referer':'{}'. Format (referer)}8     returnHeaders9 defTuji (PAG):#looking for a picture setTenFullUrl ='http://www.mzitu.com/page/{}/'. Format (PAG) Oneshouye_html =requests.get (FullUrl) AShouye_html_text =Shouye_html.text -Shouye_ele =etree. HTML (Shouye_html_text) -Tj_list = Shouye_ele.xpath ('//*[@id = "pins"]/li/a/@href')#find the map set URL for each page theTuji_url_list = [] -      forTj_urlinchtj_list: - tuji_url_list.append (Tj_url) -     returntuji_url_list + defGettuji_info (tj_url_list):#collection of URL lists to collect information about the atlas -      forTj_url_1inchTj_url_list:#tj_url_1---> http://www.mzitu.com/146823 +tj_html = Requests.get (Tj_url_1, headers=headers (tj_url_1)) ATj_html_text =Tj_html.text atTj_ele =etree. HTML (Tj_html_text) -Img_title = Tj_ele.xpath ('//h2[@class = "Main-title"]/text ()') [0]#Atlas Name -max_pag_list = Int (Tj_ele.xpath ('/html/body/div[2]/div[1]/div[4]/a[5]/span/text ()') [0])#Find maximum pages -         ifOs.path.exists (img_title) = =True: -             Print('!') -         Else: in Os.mkdir (img_title) -              forIinchRange (1, int (max_pag_list + 1)): totj_url_2 = Tj_url_1 +'/'+STR (i)#tj_url_2---> http://www.mzitu.com/146823 + Pag +tj_html = Requests.get (tj_url_2, headers=headers (tj_url_1)) -Tj_html_text =Tj_html.text theTj_ele =etree. HTML (Tj_html_text) *Img_url = Tj_ele.xpath ('//div[@class = "Main-image"]/p/a/img/@src') [0]#find the URL of a picture from a different tj_url_2 $                 Print('is downloading'+img_title+'Section'+str (i) +'Zhang')Panax NotoginsengWith open (img_title+'/'+str (i) +'. jpg',"wb+") as JPG: -Jpg.write (Requests.get (Img_url, headers=headers (tj_url_2)). Content) the  + if __name__=='__main__': Apags = Int (input ('How many pages do you want to make?')) the      forPaginchRange (1,pags+1): +Gettuji_info (Tuji (PAG))

Python Reptile Tanabata Benefits

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.