簡體   English   中英

Python-從plist / xml提取信息

[英]Python - Extract information from plist/xml

這是我的plist / xml(我已經將其縮短了一點,但是它仍然很大,因此我將其作為鏈接: https : //ghostbin.com/paste/rbu9t

我想做的是提取像這樣的字段:

appIdentifier
班級名稱
function <--- Function =“顯示名稱”之后的行。

我現在所擁有的: http : //i.imgur.com/X8wlHod.gif

但是,如果我繼續使用該模式,它將提取錯誤的內容。

我希望能夠在轉到下一個應用程序之前為每個應用程序提取所有函數和類名。

如果那不可能,那么我希望能夠提取所有類和函數,但要使用僅包含一個應用程序信息的plist / xml。

老問題:我正在編碼一個程序,但是正如您所看到的,我正在執行+6,然后+1,然后+6,然后+1。 有沒有辦法自動執行此操作(某些程序或站點)或一些代碼來幫助我? 干杯!

我的代碼:(*注:數字模式實際上並沒有我想的那么有用,因為每個應用程序可以具有不同數量的類和函數。

import xml.dom.minidom

xml = xml.dom.minidom.parse('my.plist')

Document = xml.getElementsByTagName('plist')



for key in Document:
    desc = key.getElementsByTagName('string')[5].firstChild.data
    desc1 = key.getElementsByTagName('string')[6].firstChild.data

print('Class: ', desc, 'Function: ', desc1)


desc = key.getElementsByTagName('string')[11].firstChild.data
desc1 = key.getElementsByTagName('string')[12].firstChild.data

print('Class: ', desc, 'Function: ', desc1)


desc = key.getElementsByTagName('string')[17].firstChild.data
desc1 = key.getElementsByTagName('string')[18].firstChild.data

print('Class: ', desc, 'Function: ', desc1)

desc = key.getElementsByTagName('string')[23].firstChild.data
desc1 = key.getElementsByTagName('string')[24].firstChild.data

print('Class: ', desc, 'Function: ', desc1)


desc = key.getElementsByTagName('string')[29].firstChild.data
desc1 = key.getElementsByTagName('string')[30].firstChild.data

print('Class: ', desc, 'Function: ', desc1)

抱歉,如果帖子內容不是很詳細或沒有解釋,如果您有任何問題,我會盡力回答!

x = 5
for key in Document:
    desc = key.getElementsByTagName('string')[x].firstChild.data
    desc1 = key.getElementsByTagName('string')[x+1].firstChild.data
    print('Class: ', desc, 'Function: ', desc1)
    x += 5

您可以嘗試上述代碼片段,希望對您有所幫助

使用支持XPath的現代XML庫,這要容易得多。 因此,我在下面使用lxml

import lxml.etree as etree
import sys

doc = etree.parse(open(sys.argv[1]))
for app_dict in doc.xpath('/plist/dict/array/dict'):
    appId = app_dict.xpath('./key[.="appIdentifier"]/following-sibling::string[1]/text()')[0]
    for method_dict in app_dict.xpath('.//dict[key="methodObjc"]/dict'):
        classId = method_dict.xpath('./key[.="className"]/following-sibling::string[1]/text()')[0]
        methodId = method_dict.xpath('./key[.="displayName"]/following-sibling::string[1]/text()')[0] 
        print 'App: %s; Class: %s; Method: %s' % (appId, classId, methodId)

全部輸出為:

App: com.apprizon.follow4followapp; Class: IBInAppPurchasesManager; Method: -(bool) isPremium
App: com.apprizon.follow4followapp; Class: IBRedeemCodeView; Method: -(unsigned long long) reward
App: com.apprizon.follow4followapp; Class: IBGetFollowersGoldenFollowersCell; Method: -(void) setFreeFollowers:(long long)
App: com.apprizon.follow4followapp; Class: IBGetFollowersGoldenFollowersCell; Method: -(long long) freeFollowers
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(double) extraRewardForPro
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setReferralMinReward:(long long)
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(long long) referralMinReward
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setRewardForSecondaryAccount:(long long)
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(long long) rewardForSecondaryAccount
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setExtraRewardForPro:(double)
App: com.apprizon.follow4followapp; Class: IBMe; Method: -(unsigned long long) availableSpins
App: com.apprizon.follow4followapp; Class: IBMe; Method: -(bool) canRedeemCode
App: com.apprizon.follow4followapp; Class: IBMe; Method: -(void) setCanRedeemCode:(bool)
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(bool) wofFreeSpinsEnabled
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setWofFreeSpinsEnabled:(bool)
App: com.apprizon.follow4followapp; Class: IBWheelOfFortuneView; Method: -(long long) numberOfFreeSpins
App: com.apprizon.follow4followapp; Class: IBWheelOfFortuneView; Method: -(void) setNumberOfFreeSpins:(long long)
App: com.apprizon.follow4followapp; Class: IBWheelOfFortunePrizeView; Method: -(bool) freeSpinAvailable
App: com.apprizon.follow4followapp; Class: IBWheelOfFortunePrizeView; Method: -(void) setFreeSpinAvailable:(bool)
App: com.apprizon.follow4followapp; Class: IBWheelOfFortuneManager; Method: -(bool) freeSpinAvailable
App: com.apprizon.follow4followapp; Class: IBMiniGame; Method: -(void) setPrize:(id)
App: com.apprizon.follow4followapp; Class: IBMiniGame; Method: -(id) prize
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(unsigned long long) maxEnergyDefault
App: com.apprizon.follow4followapp; Class: IBSettings; Method: -(void) setMaxEnergyDefault:(unsigned long long)
App: com.betternet; Class: BetternetUser; Method: -(bool) isPremium
App: com.betternet; Class: BetternetUser; Method: -(void) setIsPremium:(bool)
App: com.betternet; Class: BetternetUser; Method: -(bool) allowedPremium
App: com.betternet; Class: GADDevice; Method: -(bool) jailbroken
App: com.betternet; Class: FlurryUtil; Method: +(BOOL) deviceIsJailbroken
App: co.allconnected.vpnmaster; Class: FlurryUtil; Method: +(BOOL) deviceIsJailbroken
App: com.anchorfree.hss; Class: ZDKUser; Method: -(bool) isAgent
App: com.anchorfree.hss; Class: AFUserAccount; Method: -(void) setAuto_renew:(bool)
App: com.anchorfree.hss; Class: AFUserAccount; Method: -(bool) auto_renew
App: com.anchorfree.hss; Class: AFUserAccount; Method: -(void) setIsValid:(bool)
App: com.apprizon.follow4followapp; Class: IBInAppPurchasesManager; Method: -(bool) isPremium
App: com.alphaweb.fairyfail; Class: IBBearGood; Method: -(float) speed
App: com.alphaweb.fairyfail; Class: GameScene; Method: -(int) totalNumberOfStars
App: com.alphaweb.fairyfail; Class: BalloonSprite; Method: -(float) speed
App: com.alphaweb.fairyfail; Class: GameScene; Method: -(BOOL) rateMenuShouldBeDisplayed
App: com.alphaweb.fairyfail; Class: GameScene; Method: -(BOOL) isSpiderExist
App: com.alphaweb.fairyfail; Class: IBBearBomb; Method: -(BOOL) isBomb
App: com.intsig.CamScannerHDPro; Class: CSDataCenter; Method: +(bool) isPremiumAccount
App: com.intsig.CamScannerHDPro; Class: CSLoggedinViewController; Method: -(bool) isPremiumAccount
App: com.intsig.CamScannerHDPro; Class: ENSession; Method: -(bool) isPremiumUser
App: com.intsig.CamScannerHDPro; Class: CSSettings; Method: +(bool) isPremiumFeatureAvailable
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(int) faxBalance
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(void) setFaxBalance:(int)
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(bool) canSendFax
App: com.intsig.CamScannerHDPro; Class: CSStoreManager; Method: -(void) setCanSendFax:(bool)
App: com.intsig.CamScannerHDPro; Class: CSHDAppDelegate; Method: -(void) collectInfos

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM