你難道不想知道怎麼用Python爬取性感美女壁紙?

來源:互聯網
上載者:User

標籤:ges   bsp   col   page   src   爬蟲   jpg   本地   準備   

百度性感美女壁紙瞭解一下

 

看到這個圖片,有沒有一種.........emmmmm.......刺激、興奮的感覺

不管你們有沒有 反正小編我是有一股衝勁的,自從知道了Python爬蟲之後,只要看到有妹子的照片的網站,我就是想要批量下載一下!

不為別的,是為了能更好的學習Python! 我這樣說你們信嗎?

準備:

Python3.6

import requests

import json

完整代碼
 1 # !/usr/bin/env python 2 # -*- coding:utf-8 -*- 3  4 import requests 5 import json 6  7  8 # 定義一個請求函數,接收頁面參數 9 def get_page(page):10     # 把頁面參數添加在url的字串當中11     url = ‘https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=美女&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=美女&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&cg=girl&pn={}&rn=30&gsm=1e‘.format(12         page)13     # 請求網站,並且得到網站的響應14     #Python學習交流群:125240963,群內每天分享乾貨,包括最新的python企業案例學習資料和零基礎入門教程,歡迎各位小夥伴入群學習交流15     response = requests.get(url)16     # 判斷狀態的狀況17     if response.status_code == 200:18         # 返迴文本檔案資訊19         return response.text20 21 22 def json_load(text):23     # 把文字檔處理成字典格式24     jsondict = json.loads(text)25     # 建立一個空的合集,作用是去重26     urlset = set()27     # 檢查字典裡面是否包含了data這個值28     if ‘data‘ in jsondict.keys():29         # 從jsondict中取出data這個字典裡面的東西,依次賦值給items!30         for items in jsondict.get(‘data‘):31             # 異常處理,不是每一行資料都包含thumbURL這個資料的32             try:33                 urlset.add(items[‘thumbURL‘])34             except:35                 pass36     return urlset37 38 39 def down_cont(url):40     response = requests.get(url)41     name = url.split(‘,‘)[-1].split(‘&‘)[0]42     if response.status_code == 200:43         # 表示,如果檔案名稱字相同,就刪除當前檔案,然後再建立一個一樣名字的檔案44         with open(‘./images/%s.jpg‘ % name, ‘wb‘) as f:45             print(‘正在下載當前圖片: ‘ + url)46             # 以二進位的方法寫入到本地47             f.write(response.content)48 49 50 def main():51     for p in range(5):52         print(‘正在下載 %s頁 的圖片‘ % p)53         page = p * 3054         text = get_page(page)55         urlset = json_load(text)56         for url in urlset:57             down_cont(url)58 59 60 if __name__ == ‘__main__‘:61     main()
運行結果

 

你難道不想知道怎麼用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.