python3.2
# -*- coding: utf-8 -*-
#爬取王者荣耀英雄图片
#导入所需模块--安装模块pip install requests
import requests
import re
import os
#导入json文件(里面有所有英雄的名字及数字)
url='http://pvp.qq.com/web201605/js/herolist.json'
#英雄的名字
head={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
html=requests.get(url,headers=head)
html_json=html.json()
#提取英雄名字和数字
hero_name=list(map(lambda x:x['cname'],html_json))
hero_number=list(map(lambda x:x['ename'],html_json))
ii=0
for v in hero_number:
os.makedirs('D:\\heropic\\'+hero_name[ii])
os.chdir('D:\\heropic\\'+hero_name[ii])
ii=ii+1
for u in range(12):
onehero_links='http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'+str(v)+'/'+str(v)+'-bigskin-'+str(u)+'.jpg'
im = requests.get(onehero_links)
if im.status_code == 200:
iv=re.split('-',onehero_links)
open(iv[-1], 'wb').write(im.content)
python2.6
#coding:utf8
import re
import urllib2
def find_start_imooc(fname,findstr):
f = open(fname)
for line in f:
if line.startswith(findstr):
print line
#find_start_imooc("doc.txt", 'imooc')
def find_startAndEnd_imooc(fname,findstr):
f = open(fname)
for line in f:
if line.startswith(findstr) and line[:-1].endswith(findstr):
print line
#find_startAndEnd_imooc("doc.txt", 'imooc')
str1 = 'imooc hello'
#忽略大小写--re.I,返回组--(imooc)
#pa = re.compile(r'imooc',re.I)
#pa.match(str1)
#url = "http://www.imooc.com/course/list?c=python"
testurl = "http://img.ivsky.com/img/tupian/t/201010/06/aiji.png"
f = open('aiji.png','w')
req = urllib2.urlopen(testurl)
buf = req.read()
f.write(buf)
# url = "http://www.ivsky.com/tupian/geguo_guoqi_v1773/"
# req = urllib2.urlopen(url);
# buf = req.read()
#
# listurl = re.findall(r'src=.+\.png', buf)
# #save url pic
# i=0
# for url in listurl:
# f = open(str(i)+'.png','w')
# durl = re.sub(r'src=\"*', '', url)
# req = urllib2.urlopen(durl)
# buf = req.read()
# f.write(buf)
# print 'load the',i,'over'
# i+=1