From aea9c98804971301b13754a51e0a7ab8279f3920 Mon Sep 17 00:00:00 2001 From: patchlion Date: Fri, 7 Apr 2017 18:08:16 +0800 Subject: [PATCH] 0013 --- patchlion/0013/GetImageFromURL.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 patchlion/0013/GetImageFromURL.py diff --git a/patchlion/0013/GetImageFromURL.py b/patchlion/0013/GetImageFromURL.py new file mode 100644 index 00000000..0b942d62 --- /dev/null +++ b/patchlion/0013/GetImageFromURL.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +__author__ = 'PatchLion' + +from bs4 import BeautifulSoup +import requests +import os + +def loadUrl(url): + html = requests.request("GET", url) + print(html.content) + soup = BeautifulSoup(html.content, 'lxml') + list = soup.find_all('img') + #img_list = [] + rootpath = "image" + if not os.path.exists(rootpath): + os.mkdir(rootpath) + index = 0 + for value in list: + #img_list.append(value['src']) + downloadImg(value['src'], os.path.join(rootpath, str(index) + ".jpg")) + index += 1 + +def downloadImg(url, savepath): + data = requests.request('GET', url) + with open(savepath, 'wb') as f: + f.write(data.content) + +loadUrl("http://tieba.baidu.com/p/2166231880") \ No newline at end of file