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()
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
if '_fw658' in img_url:
img_url = img_url[:-6]
urllib.urlretrieve(img_url, '手机壁纸%s.jpg' % pin_id)
except:
continue
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)
imgHandle(groups)