【问题标题】:Provide network data from Firebug to Python提供从 Firebug 到 Python 的网络数据
【发布时间】:2017-01-26 06:49:03
【问题描述】:

有没有办法从 Firebug 复制网络数据(例如 POST 标头)并将它们放入 Python 代码中,这样我就不需要自己编写每个标头了?

有一个选项复制请求标头,但它的格式不适合 Python。

所以我想要的不是得到这个:

User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

因为我必须将格式更改为字典或其他内容,但是:

"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0"
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

没有必要以 Python 的字典格式获取它。我唯一想要的就是在 Python 中自动使用这些数据。

【问题讨论】:

    标签: python firebug clipboard


    【解决方案1】:

    对您从 Firefox 复制的标题进行后处理:将输入字符串的每一行按: 分割并制作一个字典,例如:

    In [1]: headers = """
    User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    """
    
    In [2]: dict(item.split(": ", 1) for item in headers.splitlines() if item)
    Out[2]: 
    {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0'}
    

    【讨论】:

    • 看起来没问题,但它返回一个错误。 dictionary = dict(item.split(": ", 1) for item in headers.splitlines() if item) ValueError: dictionary update sequence element #0 has length 1; 2 是必需的
    • firebug_headers_to_dict(""" POST /Tournaments.aspx HTTP/1.1 主机:data.skga.sk 用户代理:Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: cs,en-US;q=0.7,en;q= 0.3 Accept-Encoding: gzip, deflate Referer: data.skga.sk/Tournaments.aspx Cookie: __utma=90264207.2070959350.1431268267.1431268267.1431273199.2; __utmz=90264207.1431273199.2.2.utmcsr=skga.sk|utmccn=(referral)|utmcmd=referral|utmcct=/turnaje. php; ASP.NET_SessionId=q2bahxy0wuaxaa55j52cmdmf; __utmc=90264207 """)
    • @Milan 据我了解,您可以跳过第一行:dict(item.split(": ", 1) for item in headers.splitlines()[1:] if item)
    猜你喜欢
    • 2017-09-26
    • 2020-12-06
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多