如何根据图片url地址下载网页里的原始照片(含exif信息)

最近在爬取游记里面的图片,需要图片的原始exif信息(如gps位置,拍摄时间)用于课题研究,请问有人了解怎么办吗?如何根据图片url地址得到原始的图片呢,因为平常的下载后,exif信息都没有了

你说的这些有些属于隐私了,应该给抹掉了,没法拿到的

如果你想根据图片 URL 地址下载网页里的原始照片并保留其 exif 信息,可以考虑以下几种方法:

使用 Python 的 requests 库发送 GET 请求下载图片,并使用 pillow 库读取 exif 信息。

使用 Python 的 wget 模块下载图片,并使用 piexif 库读取 exif 信息。

使用 Python 的 selenium 库模拟浏览器下载图片,并使用 pillow 库读取 exif 信息。

代码如下:


```python
import requests
from PIL import Image

url = "https://example.com/image.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
exif_data = img._getexif()
这样就可以得到图片的原始exif信息了,然后可以根据你的需要进行处理,如抽取gps位置、拍摄时间等信息。

注意:这些方法需要事先安装requests,pillow,piexif,wget,selenium等第三方库。

```

望采纳!!!

EXIF.py:https://github.com/ianare/exif-py

img

这个是要看服务端的。

服务端存储图片时,如果将图片进行压缩、去除了exif信息,那你下载后肯定就没有这些信息,服务端不是你开发的话这个是没办法解决的。

服务端存储图片时,如果不压缩,那你下载的图片就可能包含这些信息。

如果图片里包含exif信息,怎么读取网上找一下有很多
https://www.baidu.com/s?wd=python%20%E8%8E%B7%E5%8F%96%20exif

Python Imaging Library

from PIL import Image
from PIL.ExifTags import TAGS
def get_exif_data(fname):
    """Get embedded EXIF data from image file."""
    ret = {}
    try:
        img = Image.open(fname)
        if hasattr( img, '_getexif' ):
            exifinfo = img._getexif()
            if exifinfo != None:
                for tag, value in exifinfo.items():
                    decoded = TAGS.get(tag, tag)
                    ret[decoded] = value
    except IOError:
        print 'IOERROR ' + fname
    return ret

if __name__ == '__main__':
    fileName = '图片.jpg'
    exif = get_exif_data(fileName)
    print exif

根据运行结果查看相关信息

a标签里的murl属性,后缀为.jpg的那个网址

没有办法拿到的!

例1:

import requests
from PIL import Image
 
url = "https://example.com/image.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))

例2:

from PIL import Image
from PIL.ExifTags import TAGS
def get_exif_data(fname):
    ret = {}
    try:
        img = Image.open(fname)
        if hasattr( img, '_getexif' ):
            exifinfo = img._getexif()
            if exifinfo != None:
                for tag, value in exifinfo.items():
                    decoded = TAGS.get(tag, tag)
                    ret[decoded] = value
    except IOError:
        print 'IOERROR ' + fname
    return ret
 
if __name__ == '__main__':
    fileName = 'image.jpg'
    exif = get_exif_data(fileName)
    print exif

例3:
用exif库

if __name__ == '__main__':
    import sys
    import getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hqsdt:v", ["help", "quick", "strict", "debug", "stop-tag="])
    except getopt.GetoptError:
        usage(2)
    if args == []:
        usage(2)
    detailed = True
    stop_tag = 'UNDEF'
    debug = False
    strict = False
    for o, a in opts:
        if o in ("-h", "--help"):
            usage(0)
        if o in ("-q", "--quick"):
            detailed = False
        if o in ("-t", "--stop-tag"):
            stop_tag = a
        if o in ("-s", "--strict"):
            strict = True
        if o in ("-d", "--debug"):
            debug = True
    # 输出每个文件的信息
    for filename in args:
        try:
            file=open(filename, 'rb')
        except:
            print "'%s' is unreadable\n"%filename
            continue
        print filename + ':'
        # 拿到标签
        data = process_file(file, stop_tag=stop_tag, details=detailed, strict=strict, debug=debug)
        if not data:
            print 'No EXIF information found'
            continue
        x=data.keys()
        x.sort()
        for i in x:
            if i in ('JPEGThumbnail', 'TIFFThumbnail'):
                continue
            try:
                print '   %s (%s): %s' % \
                      (i, FIELD_TYPES[data[i].field_type][2], data[i].printable)
            except:
                print 'error', i, '"', data[i], '"'
        if 'JPEGThumbnail' in data:
            print 'File has JPEG thumbnail'
        print

网上的图片都是经过处理的,没有那些图片信息