图片的批量抓取是爬虫批处理的典型例子,这里以煎蛋网美女图片为抓取对象,使用Python实现批量获取图片。
批量抓取程序
import urllib.request
import os
import random
# 打开网页
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36')
response = urllib.request.urlopen(url)
html = response.read()
return html
# 找到网页地址
def get_page(url):
html = url_open(url).decode('utf-8')
a = html.find('current-comment-page') + 23
b = html.find(']',a)
print (html[a:b])
return html[a:b]
# 找到图片的地址
def find_imgs(url):
html =