Python爬蟲之——爬取妹子圖片

來源:互聯網
上載者:User

標籤:ext   coding   class   文本   ado   agent   code   msi   準備   

#coding=utf-8import requestsfrom bs4 import BeautifulSoupimport osall_url = ‘http://www.mzitu.com‘#http要求標頭Hostreferer = { ‘User-Agent‘:‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)‘, ‘Referer‘:‘http://www.mzitu.com‘ }Picreferer = { ‘User-Agent‘:‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)‘, ‘Referer‘:‘http://i.meizitu.net‘}#此要求標頭破解盜鏈start_html = requests.get(all_url,headers = Hostreferer)#儲存地址path = ‘G:\python\妹子\liang‘#找尋最大頁數soup = BeautifulSoup(start_html.text,"html.parser")page = soup.find_all(‘a‘,class_=‘page-numbers‘)max_page = page[-2].textsame_url = ‘http://www.mzitu.com/page/‘for n in range(1,int(max_page)+1): ul = same_url+str(n) start_html = requests.get(ul, headers = Hostreferer) soup = BeautifulSoup(start_html.text,"html.parser") all_a = soup.find(‘div‘,class_=‘postlist‘).find_all(‘a‘,target=‘_blank‘) for a in all_a: title = a.get_text() #提取文本 if(title != ‘‘): print("準備扒取:"+title) #win不能建立帶?的目錄 if(os.path.exists(path+title.strip().replace(‘?‘,‘‘))): #print(‘目錄已存在‘) flag=1 else: os.makedirs(path+title.strip().replace(‘?‘,‘‘)) flag=0 os.chdir(path + title.strip().replace(‘?‘,‘‘)) href = a[‘href‘] html = requests.get(href,headers = Hostreferer) mess = BeautifulSoup(html.text,"html.parser") pic_max = mess.find_all(‘span‘) pic_max = pic_max[10].text #最大頁數 if(flag == 1 and len(os.listdir(path+title.strip().replace(‘?‘,‘‘))) >= int(pic_max)): print(‘已經儲存完畢,跳過‘) continue for num in range(1,int(pic_max)+1): pic = href+‘/‘+str(num) html = requests.get(pic,headers = Hostreferer) mess = BeautifulSoup(html.text,"html.parser") pic_url = mess.find(‘img‘,alt = title) print(pic_url[‘src‘]) #exit(0) html = requests.get(pic_url[‘src‘],headers = Picreferer) file_name = pic_url[‘src‘].split(r‘/‘)[-1] f = open(file_name,‘wb‘) f.write(html.content) f.close() print(‘完成‘) print(‘第‘,n,‘頁完成‘)

Python爬蟲之——爬取妹子圖片

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.