Skip to content

Instantly share code, notes, and snippets.

@joyc
Last active May 14, 2017 12:44
Show Gist options
  • Save joyc/8d7819f68d07aaf18ee573f72b1baa03 to your computer and use it in GitHub Desktop.
Save joyc/8d7819f68d07aaf18ee573f72b1baa03 to your computer and use it in GitHub Desktop.
download ranking photos from wear.jp
#! python3
# -*- coding:utf-8 -*-
# @Time : 2017/05/13 19:06
# @Author : Hython.com
# @File : wear.py - Download the new ranking image from wear.jp
# @IDE : PyCharm
import requests, os, bs4, sys
# 要下载图片的页面地址
keyword = ''.join(sys.argv[1:])
if keyword == '':
rankPage = 'http://wear.jp/ranking/'
else:
rankPage = 'http://wear.jp/' + keyword + '-ranking/'
# 保存到指定目录
os.chdir('C:\\Users\\rockb_000\\Desktop\\Wear-Photo')
print('已保存到目录-→ ' + os.getcwd())
os.makedirs('wear-' + keyword + '-ranking', exist_ok=True)
while not rankPage.endswith('#'):
print('下载图片中。。。')
# 下载整个页面
res = requests.get(rankPage)
try:
res.raise_for_status()
except Exception as exc:
print('There was a problem: %s' % (exc))
soup = bs4.BeautifulSoup(res.text, "html.parser")
# 查找图片地址并保存
imgElem = soup.select('div.image > a > p > img')
if imgElem == []:
print('什么也没找到。。。')
else:
for i in range(len(imgElem)):
url = imgElem[i].get('src')
res = requests.get(url)
res.raise_for_status()
imgFile = open(os.path.join('wear-' + keyword + '-ranking', os.path.basename(url)), 'wb')
for chunk in res.iter_content(100000):
imgFile.write(chunk)
imgFile.close()
rankPage = '#'
print('下载结束!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment