defget_img_list(url): # 獲取一頁上的圖片 defget_img(page_url): return parser(page_url).find_all('img', class_='BDE_Image') page = parser(url).find_all('li', class_='l_reply_num')[0].find_all('span')[1].text img_list = [] # 獲取每一頁上的圖片 for i in range(1, int(page) + 1): img_list += [x for x in get_img(url + '&pn=' + str(i))] return img_list
# 從圖片列表中下載圖片 defdownload_img(img_list): n = 0 for img in img_list: n += 1 img_url = requests.get(img['src'], stream=True) with open('./' + str(n) + '.jpg', 'wb') as f: start = time.time() f.write(img_url.content) end = time.time() print('%s/%s file(s) downloaded in %ss.' % (str(n), len(img_list), end - start)) return
if __name__=='__main__': # 魔女卡片帖:https://tieba.baidu.com/p/1180021348?see_lz=1 # 悲歎之種帖:https://tieba.baidu.com/p/6393683868?see_lz=1 url = input('Input the page url: ') download_img(get_img_list(url)) print('done.')