def open_url(url):
req = urlRe.Request(url) #請求
req.add_header('User-Agent',' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36')
response = urlRe.urlopen(req) #接收響應數據
html = response.read().decode('gbk')#將響應數據存在html並轉為utf-8編碼
#print(str(html))
return html
def get_img(html):
p = r'<img src="(.+?.jpg)"'
img = re.compile(p)
imglist = re.findall(img,html)
for each in imglist:
print(each)
return imglist
def save_image(img_list):# acquisition the picture and save in a file
file_dir = os.getcwd() #get path dir
file_path = os.path.join(file_dir,'download_image') #create a new file
exists_file = os.path.exists(file_path) #check if the file exists or not
if not exists_file:
os.mkdir(file_path) #create a new file
print(file_path)
x = 1
for each in img_list:
pic_url = 'http://pic.netbian.com'+ each
urlRe.urlretrieve(pic_url ,file_path+'/%s.jpg' % x)
x += 1
return file_path
urlRe.urlretrieve(pic_url ,file_path+'/%s.jpg' % x)