python 爬图第一次
结果如图:

代码如下:
import os.path
from re import findall
from urllib.request import urlopen
from urllib.request import Request
path='D:/python-ppt/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
if not os.path.exists(path):
os.mkdir(path)
url = 'http://www.meizitu.com/a/5583.html'
req = Request(url=url, headers=headers)
with urlopen(req) as fp:
content = fp.read().decode('gbk')
pattern = 'src="(.+?)" /><br />'
result = findall(pattern, content)
for index, item in enumerate(result):
print(item)
itemreq = Request(url=str(item), headers=headers)
with urlopen(itemreq) as fp:
with open(path+str(index)+'.jpg','wb') as f:
f.write(fp.read())
print('结束')
浙公网安备 33010602011771号