Python爬虫|爬取煎蛋网妹子图

教程来自鱼C,仅使用urllib模块和正则

# !/usr/bin/env python3
# -*- coding:utf-8 -*-
import urllib.request
import os
import re

def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read()
return html
def get_page(url):
html = url_open(url).decode('utf-8')
#print(html)

reg = r'<span class="current-comment-page">\[(\d+)\]</span>'
imglist = re.findall(reg,html)

return imglist[0]
def find_imgs(url):
html = url_open(url).decode('utf-8')
reg = r'<img src="(//.*?\.jpg)"'
imglist = re.findall(reg,html)
img_addrs = []

for i in imglist:
img_addrs.append('http:'+i)
return img_addrs

def save_imgs(folder,img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename,'wb') as f:
img = url_open(each)
f.write(img)
def download_mm(folder='OOXX',pages=169):
url = 'http://jandan.net/ooxx/'
os.mkdir(folder)
os.chdir(folder)
page_num = int(get_page(url))

for i in range(pages):
page_num -= 1
page_url = url + 'page-' + str(page_num) + '#comments'
img_addrs = find_imgs(page_url)
save_imgs(folder,img_addrs)

if __name__ == '__main__':

download_mm()

 

 

效果图

Python爬虫|爬取煎蛋网妹子图

分享