1

解析する必要があります

http://www.webpagetest.org/breakdown.php?test=150325_34_0f581da87c16d5aac4ecb7cd07cda921&run=2&cached=0

上記のURLのソースを表示すると、

期待される出力:

fvRequests= css
fvRequests=7
4

2 に答える 2

1

アイデアは、スクリプトを見つけてBeautifulSoup正規表現パターンを使用してfvRequests.setValue()呼び出しを見つけ、3 番目の引数の値を抽出することです。

import re

from bs4 import BeautifulSoup
import requests


pattern = re.compile(r"fvRequests\.setValue\(\d+, \d+, '?(\w+)'?\);")

response = requests.get("http://www.webpagetest.org/breakdown.php?test=150325_34_0f581da87c16d5aac4ecb7cd07cda921&run=2&cached=0")
soup = BeautifulSoup(response.content)

script = soup.find("script", text=lambda x: x and "fvRequests.setValue" in x).text
print(re.findall(pattern, script))

版画:

[u'css', u'7', u'flash', u'0', u'font', u'0', u'html', u'14', u'image', u'80', u'js', u'35', u'other', u'14']

さらに進んで、リストを dict にパックすることができます (ソリューションはhereから取得):

dict(zip(*([iter(data)] * 2)))

これは以下を生成します:

{
    'image': '80', 
    'flash': '0', 
    'js': '35', 
    'html': '14',  
    'font': '0', 
    'other': '14', 
    'css': '7'
}
于 2015-04-05T07:52:49.373 に答える