爬取图片方法一: requests
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import re
import requests
import os
import shutil
suyan_url= 'https://www.xiaohuar.com/s-1-2069.html'
response = requests.get(suyan_url)
# print(response.content)
lists = re.findall(r'href="/go.html?url=(?:.*?)".*?class="(?:.*?)"',response.text,re.S) #re.S 把文本信息转换成1行匹配
folder = 'aabb'
if os.path.exists(folder):
shutil.rmtree(folder)
os.mkdir(folder);
os.chdir(folder);
for each in lists:
imgurl = each.split(' ')[0][6:-1]
if imgurl[-4:] == '.jpg':
filename = imgurl.split('/')[-1]
img = requests.get(imgurl)
with open(filename,'wb') as f:
f.write(img.content)#!/usr/bin/python2.7