我正在尝试解析http://www.apkmirror.com之类的APK下载页面,例如http://www.apkmirror.com/apk/google-inc/gmail/gmail-7-3-26-152772569-release-release/gmail-7-3-26-152772569-release-android-apk-download/。通常,“ APK详细信息”部分具有以下结构:
我想将“ 17329196”解析为version_code
,将“ arm”解析为architecture
,将“ com.skype.m2”解析为package
。但是有时,缺少architecture
的行如下所示:
到目前为止,将Scrapy与选择器一起使用
apk_details = response.xpath('//*[@title="APK details"]/following-sibling::*[@class="appspec-value"]//text()').extract()
我已经能够提取包含上面显示的“行”的列表。我正在尝试编写一个函数
parse_apk_details
,以便通过以下测试:import pytest
def test_parse_apk_details_with_architecture():
apk_details = [u'Version: 3.0.38_ww (4030038)',
u'arm ',
u'Package: com.lenovo.anyshare.gps',
u'\n',
u'2,239 downloads ']
version_code, architecture, package = parse_apk_details(apk_details)
assert version_code == 4030038
assert architecture == "arm"
assert package == "com.lenovo.anyshare.gps"
@pytest.mark.skip(reason="This does not work yet, because 'Package:' is interpreted by the parser as the architecture.")
def test_parse_apk_details_without_architecture():
apk_details = [u'Version: 3.0.38_ww (4030038)',
u'Package: com.lenovo.anyshare.gps',
u'\n',
u'2,239 downloads ']
version_code, architecture, package = parse_apk_details(apk_details)
assert version_code == 4030038
assert package == "com.lenovo.anyshare.gps"
if __name__ == "__main__":
pytest.main([__file__])
但是,如上所述,第二项测试尚未通过。这是到目前为止的功能:
from pyparsing import Word, printables, nums, Optional
def parse_apk_details(apk_details):
apk_details = "\n".join(apk_details) # The newline character is ignored by PyParsing (by default)
version_name = Word(printables) # The version name can consist of all printable, non-whitespace characters
version_code = Word(nums) # The version code is expected to be an integer
architecture = Word(printables)
package = Word(printables)
expression = "Version:" + version_name + "(" + version_code("version_code") + ")" + Optional(architecture("architecture")) + "Package:" + package("package")
result = expression.parseString(apk_details)
return int(result.get("version_code")), result.get("architecture"), result.get("package")
当我尝试运行第二个测试时出现的错误是:
ParseException: Expected "Package:" (at char 38), (line:2, col:10)
我相信正在发生的事情是作品“ Package:”被“消耗”为
architecture
。解决此问题的一种方法是将行architecture = Word(printables)
更改为(用伪代码)architecture = Word(printables) + ~"Package:"
之类的内容,以表明该行可以是除单词“ Package:”以外的任何可打印字符组成的内容。如何确定
architecture
仅当不是特定单词"Package:"
时才被解析? (对于原始问题,我也会对基于scrapy
的替代解决方案感兴趣)。 最佳答案
您真的很接近architecture = Word(printables) + ~Literal("Package:")
。要进行否定的前瞻,请从否定开始,然后进行匹配:
architecture = ~Literal("Package:") + Word(printables)
关于python - 在PyParsing中,如何指定单词不等于给定的文字?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43475793/