原創:Python爬蟲實戰之爬取美女照片


  這個素材是出自小甲魚的python教程,但源碼全部是我原創的,所以,猥瑣的不是我

  注:沒有用header(總會報錯),暫時不會正則表達式(馬上要學了),以下代碼可能些許混亂,不過效果還是可以的。

  爬蟲目標網站:http://jandan.net/ooxx/ #如有侵權請聯系我

  代碼如下

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 import urllib.request
 4 import base64
 5 import time
 6 
 7 url_a = 'http://jandan.net/ooxx/'#網站前一部分
 8 need = '20200217-'#后一部分
 9 need_a = 147#后一部分的后一部分
10 
11 
12 def base(word):#base64編碼
13     outcome = base64.b64encode(word.encode("utf-8"))#編碼后
14     url = url_a + outcome.decode('utf-8') + '#comments'
15     web(url)
16 
17 def web(url):
18     response = urllib.request.urlopen(url)
19     html = response.read().decode('utf-8')
20     a = html.find('<img src="//')#從這個下標開始
21     i = 0#用來區別文件名
22     while a != -1:
23         b = html.find('.jpg',a,a+250)#到這個下標為止
24         if b != -1:
25             c = 'http://' + html[a+12:b+4]#圖片網址    
26             name = 'picture\\' + str(i) + str(need_a) + '.jpg'#文件名
27             get_jpg(c,name)
28         else:
29             b = a + 12
30         a = html.find('<img src="//',b)#循環查找
31         i += 1
32 
33 def get_jpg(address,filename):
34     get = urllib.request.urlopen(address)#打開新網址
35     jpg = get.read()
36     with open(filename,'wb') as f:#寫入圖片
37         f.write(jpg)
38     print("Succeed!")
39 
40 while need_a >=100:#逐減
41     addition = need + str(need_a)
42     base(addition)
43     need_a -= 1
44     time.sleep(10)#停頓防止被反爬

 效果圖:

  未經博主允許,不得轉載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM