爬取花瓣网精美图片

Author Avatar
Yoo 3月 01, 2017
  • 在其它设备中阅读本文章

直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# coding:utf-8
import urllib, urllib2, re, sys, os, requests
path = r"/Users/lijunyu/PycharmProjects/Dig/手机壁纸"
url = 'http://huaban.com/explore/chengshijianzhubizhi/'
i_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"}
count = 0
#### 获取网页源码
def getHtml(url):
page = urllib.urlopen(url)
html = page.read().decode('utf-8') # 需要解码
return html
def urlHandle(url):
req = urllib2.Request(url, headers=i_headers)
html = urllib2.urlopen(req).read()
# print html
reg = re.compile(r'"pin_id":(\d+),.+?"file":{"id":(\d+), "farm":"farm1", "bucket":"hbimg",.+?"key":"(.*?)",.+?"type":"image/(.*?)"', re.S)
groups = re.findall(reg, html)
print groups.__len__()
return groups
def imgHandle(groups):
if groups:
for att in groups:
pin_id = att[0]
#### 获取跳转网页网址
url_str = r'http://huaban.com/pins/%s/' % pin_id
#### 获取点击图片时弹出网页的源码
pin_id_source = getHtml(url_str)
#### 解析源码,获取原图片的网址
img_url_re = re.compile('main-image.*?src="(.*?)"', re.S)
img_url_list = re.findall(img_url_re, pin_id_source)
try:
img_url = 'http:' + img_url_list[0]
print img_url
#### 获取原图片的网址,以_fw658结尾的链接并不是原图片的链接,需要把_fw658去掉
if '_fw658' in img_url:
img_url = img_url[:-6]
urllib.urlretrieve(img_url, '手机壁纸%s.jpg' % pin_id)
except:
# print("获取图片:%s失败,跳过,获取下一张。" % img_url)
continue
# print("获取:%s成功!" % img_url)
groups = urlHandle(url)
imgHandle(groups)
while (groups):
count += 1
print count
pin_id = groups[-1][0]
print pin_id
urltemp = url + '/?max=' + str(pin_id) + '&limit=' + str(20) + '&wfl=1'
print(urltemp)
groups = urlHandle(urltemp)
# print groups
imgHandle(groups)

参考文献:

Python爬花瓣网图片
爬取花瓣网美女图片