python - 通过 Python 从给定的 URL 获取加载的图像

标签 python image url fiddler

有没有办法通过 Python 加载 URL,然后检索通过该 URL 加载的所有图像的列表?我本质上是想做一些类似于 TamperData 或 Fiddler 的事情,并检索给定网站加载的所有图像的列表。

最佳答案

有趣的任务。这是解决它的一种方法,按照 Jochen Ritzel 的建议。

它使用 pylibpcap而不是 pycap .就个人而言,我发现 pycap 很难使用,因为可用的文档很少。对于 pylibpcap,您可以直接从 libpcap 示例中翻译大部分代码(参见示例 this tutorial 以获得很好的介绍)。 tcpdump 的手册页和 pcap也是很好的资源。

您可能需要查看 Ethernet 的标准, IPv4 , TCP , 和 HTTP .

注意 1:下面的代码只打印出 HTTP GET 请求。过滤掉图像并使用 urllib module 下载它们应该没有问题。

注意 2:此代码适用于 Linux,不确定您需要在 Windows/MacOS 上使用什么设备名称。您还需要 root 权限。

#!/usr/bin/env python

import pcap
import struct

def parse_packet(data):
    """
    Parse Ethernet/IP/TCP packet.
    """
    # See the Ethernet, IP, and TCP standards for details.

    data = data[14:] # Strip Ethernet header

    header_length = 4 * (ord(data[0]) & 0x0f) # in bytes
    data = data[header_length:]  # Strip IP header

    dest_port = struct.unpack('!H', data[2:4])[0]
    if not dest_port == 80: # This is an outgoing package
        return

    header_length = 4 * ((ord(data[12]) & 0xf0) >> 4) # in bytes
    data = data[header_length:] # Strip TCP header

    return data


def parse_get(data):
    """
    Parse a HTTP GET request, returning the request URI.
    """
    if data is None or not data.startswith('GET'):
        return

    fields = data.split('\n')
    uri = fields[0].split()[1]

    for field in fields[1:]:
        if field.lower().startswith('host:'):
            return field[5:].strip() + uri


def packet_handler(length, data, timestamp):
    uri = parse_get(parse_packet(data))
    if not uri is None:
        print uri


# Set up pcap sniffer
INTERFACE = 'wlan0'
FILTER = 'tcp port 80'
p = pcap.pcapObject()
p.open_live(INTERFACE, 1600, 0, 100)
p.setfilter(FILTER, 0, 0)

try:
    while True:
        p.dispatch(1, packet_handler)
except KeyboardInterrupt:
    pass

关于python - 通过 Python 从给定的 URL 获取加载的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6522660/

相关文章:

python - ujson 无法编码 numpy 数组

javascript - python :urlParse错误?解析器 Python 3

java - 如何在Java中创建一个巨大的.png加入较小的.png?

c# - 如何创建可用的 URL 来打开文档?

javascript - 通过 javascript 生成的 URL 传递 &?

python - 按非字典顺序对结果进行排序?

python - 获取二维网格(多维列表)中具有最大值的所有图 block

.net - 我的图像很模糊!为什么 WPF 的 SnapsToDevicePixels 不起作用?

css - 在响应式 div 中垂直居中图像

ruby-on-rails - Rails,如何从 Controller 提交 PayPal 请求,而不是表单,URL 编码