【问题标题】:Obtaining loaded images from a given URL via Python通过 Python 从给定的 URL 获取加载的图像
【发布时间】:2011-09-25 05:52:54
【问题描述】:

是否可以通过 Python 加载 URL,然后检索通过该 URL 加载的所有图像的列表?我实际上是在寻找类似于 TamperData 或 Fiddler 的东西,并检索给定网站加载的所有图像的列表。

【问题讨论】:

  • 我不确定您到底需要什么。从 url 和 grep 中保存 html 内容以获取 img 标签。您是否正在寻找一个库来执行此操作并返回图像/图像 URL 列表?
  • 在我看到的情况下,网页都是在 Flash 中完成的。 TamperData 能够在页面加载时看到图像请求,但这些图像不包含在页面源中。这有意义吗?
  • 您可能无法从 swf 文件中获取 url。您所能做的就是捕获 HTTP 流量并直接复制图像。请参阅pycap 或只是从wireshark“导出对象”。

标签: python image url fiddler


【解决方案1】:

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

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

您可能想查看EthernetIPv4TCPHTTP 的标准。

注意 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

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多