[英]How to extract a javascript variable using regular expression
我需要使用python腳本(2.7)從遠程頁面提取包含多行JSON的javascript變量,我想使用正則表達式執行此操作,但我的模式不返回任何內容
我究竟做錯了什么 ?
這是我的代碼:
request = urllib2.Request("http://somesite.com/affiliates/")
result = urllib2.urlopen(request)
affiliates = re.findall('#var affiliates = (.*?);\s*$#m', result.read())
print affiliates
如果你查看re.findall(pattern, string, flags=0)
的文檔re.findall(pattern, string, flags=0)
,你會發現你需要改變你使用它的方式
affiliates = re.findall('var affiliates = (.*?);\s*$', result.read(), re.M)
您可能還想考慮JavaScript中的空格是如何草率的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.