• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用Python保存网页上的图片或者保存页面为截图

python 搞代码 4年前 (2022-01-08) 31次浏览 已收录 0个评论

这篇文章主要介绍了使用Python保存网页上的图片或者保存页面为截图的方法,保存网页图片主要用到urllib模块,即简单的爬虫原理,需要的朋友可以参考下

Python保存网页图片
这个是个比较简单的例子,网页中的图片地址都是使用’http://。。。。.jpg-600’这种方式直接定义的。

使用前,可以先建立好一个文件夹用于保存图片,本例子中使用的文件夹是 d:\\pythonPath这个文件夹

代码如下:

 # -*- coding: UTF-8 -*- import os,re,urllib,uuid #首先定义云端的网页,以及本地保存的文件夹地址 urlPath='http://gamebar.com/' localPath='d:\\pythonPath' #从一个网页url中获取图片的地址,保存在 #一个list中返回 def getUrlList(urlParam): urlStream=urllib.urlopen(urlParam) htmlString=urlStream.read() if( len(htmlString)!=0 ): patternString=r'http://.{0,50}\.jpg-600' searchPattern=re.compile(patternString) imgUrlList=searchPattern.findall(htmlString) return imgUrlList #生成一个文件名字符串 def generateFileName(): return str(uuid.uuid1()) #根据文件名创建文件 def createFileWithFileName(localPathParam,fi<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码网</strong>leName): totalPath=localPathParam+'\\'+fileName if not os.path.exists(totalPath): file=open(totalPath,'a+') file.close() return totalPath #根据图片的地址,下载图片并保存在本地 def getAndSaveImg(imgUrl): if( len(imgUrl)!= 0 ): fileName=generateFileName()+'.jpg-600' urllib.urlretrieve(imgUrl,createFileWithFileName(localPath,fileName)) #下载函数 def downloadImg(url): urlList=getUrlList(url) for urlString in urlList: getAndSaveImg(urlString) downloadImg(urlPath) 

保存的文件如下:

网页的一部分保存为图片
主要思路是selenium+phantomjs(中文网页需要设置字体)+PIL切图

 def webscreen(): url = 'http://www.xxx.com' driver = webdriver.PhantomJS() driver.set_page_load_timeout(300) driver.set_window_size(1280,800) driver.get(url) imgelement = driver.find_element_by_id('XXXX') location = imgelement.location size = imgelement.size savepath = r'XXXX.png-600' driver.save_screenshot(savepath) im = Image.open(savepath) left = location['x'] top = location['y'] right = left + size['width'] bottom = location['y'] + size['height'] im = im.crop((left,top,right,bottom)) im.save(savepath) 

以上就是使用Python保存网页上的图片或者保存页面为截图的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用Python保存网页上的图片或者保存页面为截图
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址