Python - 从plist / xml中提取信息

时间:2017-05-20 16:51:20

标签: python

这是我的plist / xml(我已经缩短了一点,但它仍然很大,所以我把它作为一个链接:https://ghostbin.com/paste/rbu9t

我想要做的是提取如下字段:

appIdentifier
类名
function < --- Function =" displayname"之后的行。

我现在得到了什么: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)

很抱歉,如果帖子不详细或解释过,如果您有任何问题,我会尽可能好地回复它们!

2 个答案:

答案 0 :(得分:2)

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

你可以尝试上面的代码片段,希望这有帮助吗

答案 1 :(得分:1)

使用支持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