【Python】python3实现网页爬虫下载图片
2017-11-02 13:36
811 查看
import re import urllib.request # ------ 获取网页源代码的方法 --- def getHtml(url): page = urllib.request.urlopen(url) html = page.read() return html # ------ getHtml()内输入任意帖子的URL ------ html = getHtml("https://tieba.baidu.com/p/5352556650") # ------ 修改html对象内的字符编码为UTF-8 ------ html = html.decode('UTF-8') # ------ 获取帖子内所有图片地址的方法 ------ def getImg(html): # ------ 利用正则表达式匹配网页内容找到图片地址 ------ reg = r'src="([.*\S]*\.jpg)"' imgre = re.compile(reg); imglist = re.findall(imgre, html) return imglist imgList = getImg(html) imgName = 0 for imgPath in imgList: # ------ 这里最好使用异常处理及多线程编程方式 ------ try: f = open('D:\\Temp\\'+ str(imgName)+".jpg", 'wb') f.write((urllib.request.urlopen(imgPath)).read()) print(imgPath) f.close() except Exception as e: print(imgPath+" error") imgName += 1 print("All Done!")
相关文章推荐
- Python 爬虫5——爬取并下载网页指定规格的图片
- Python爬虫实现抓取网页图片
- Python3实现Web网页图片下载
- [python][爬虫]从网页中下载图片
- python自学笔记(8)--Python简单爬虫从网站上下载图片和用第三方库request实现百度翻译
- python脚本工具-1 制作爬虫下载网页图片
- python实现爬虫下载美女图片
- Python爬虫实现抓取京东店铺信息及下载图片功能示例
- python爬虫一:必应图片(从网页源代码中找出图片链接然后下载)
- python简易爬虫来实现自动图片下载
- python实现爬虫下载美女图片
- python 网页爬虫,下载网络图片
- Python3.x爬虫下载网页图片的实例讲解
- Python 爬虫5——爬取并下载网页指定规格的图片
- python实现爬虫下载美女图片
- Python 下载网页 Jpg 图片以及 Gif 图片实现
- python3.6 urllib.request库实现简单的网络爬虫、下载图片
- python爬虫(1)下载任意网页图片
- python实现爬虫下载美女图片
- Python 爬虫学习 网页图片下载