|
发表于 2018-9-30 08:03:51
|
显示全部楼层
本帖最后由 塔利班 于 2018-9-30 08:04 编辑
- import urllib.request
- import re
- def get_html(url):
- page = urllib.request.urlopen(url)
- html_code = page.read().decode()
- return html_code
- def get_image(html_code):
- reg = r'src="(.+?\.jpg)" width'
- reg_img = re.compile(reg)
- img_list = reg_img.findall(html_code)
- x = 0
- for img in img_list:
- urllib.request.urlretrieve(img, '%s.jpg' % x)
- x += 1
- print(u'-------网页图片抓取-------')
- url = input(u'请输入url:',)
- if url:
- pass
- else:
- print(u'---没有地址输入正在使用默认地址---')
- url = 'http://tieba.baidu.com/p/1753935195'
- print(u'----------正在获取网页---------')
- html_code = get_html(url)
- print(u'----------正在下载图片---------')
- get_image(html_code)
- print(u'-----------下载成功-----------')
- input('Press Enter to exit')
复制代码 |
|