从Python/xm提取信息

2024-04-20 14:30:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

应用标识符
类名
function<;---function=显示名称后的行。你知道吗

我现在得到的是:http://i.imgur.com/X8wlHod.gif

但如果我继续这个模式,它会提取错误的东西。你知道吗

我想能够extarct所有函数和类名为每个应用程序之前,我去下一个。你知道吗

如果这是不可能的,那么我希望能够提取所有类和函数,但是使用plist/xml,它只包含一个应用程序的信息。你知道吗

OLD QUESTION: I'm coding a program, but as you can see, I'm doing +6 then +1, then +6, then +1. Is there a way to automate this (some program or site) or some code to help me? Cheers!

我的代码:(*注意:数字模式并没有我想象的那么有用,因为每个应用程序可以有不同数量的类和函数。你知道吗

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)

对不起,如果帖子不是很详细或解释,如果你有任何问题,我会尽我所能回答他们!你知道吗


Tags: key函数应用程序datastringfunctionxmldesc
2条回答

使用支持XPath的现代XML库,这要容易得多;因此,我使用下面的^{}。你知道吗

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
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

你能试试上面的代码片段吗,希望对你有所帮助

相关问题 更多 >