【问题标题】:Generate DF from attributes of tags in list从列表中标签的属性生成 DF
【发布时间】:2022-01-17 16:41:18
【问题描述】:

我有一个维基百科文章的修订列表,我这样查询:

import urllib
import re

def getRevisions(wikititle):
    url = "https://en.wikipedia.org/w/api.php?action=query&format=xml&prop=revisions&rvlimit=500&titles="+wikititle 
    revisions = []                                        #list of all accumulated revisions
    next = ''                                             #information for the next request

    while True:
        response = urllib.request.urlopen(url + next).read()     #web request

        response = str(response)

        revisions += re.findall('<rev [^>]*>', response)  #adds all revisions from the current request to the list

        cont = re.search('<continue rvcontinue="([^"]+)"', response)
        if not cont:                                      #break the loop if 'continue' element missing
            break

        next = "&rvcontinue=" + cont.group(1)             #gets the revision Id from which to start the next request
    return revisions    

这会产生一个列表,其中每个元素都是 rev 标记作为字符串:

['<rev revid="343143654" parentid="6546465" minor="" user="name" timestamp="2021-12-12T08:26:38Z" comment="abc" />',...]

我怎样才能从这个列表中生成一个 DF

【问题讨论】:

    标签: python python-3.x pandas wikipedia mediawiki-api


    【解决方案1】:

    不使用正则表达式的“简单”方法是拆分字符串然后解析:

    for rev_string in revisions:
        rev_dict = {}
    
        # Skipping the first and last as it's the tag.
        attributes = rev_string.split(' ')[1:-1]
    
        #Split on = and take each value as key and value and convert value to string to get rid of excess ""
        for attribute in attributes:
            key, value = attribute.split("=")            
            rev_dict[key] = str(value) 
        
        df = pd.DataFrame.from_dict(rev_dict)
    

    此示例将为每个修订创建一个数据框。如果您想在一个字典中收集多个版本,那么您可以处理独特的属性(我不知道这些属性是否会根据 wiki 文档而变化),然后在收集字典中的所有属性后转换为 DataFrame。

    【讨论】:

      【解决方案2】:

      使用json的输出格式,那么你可以很容易地从Json创建数据帧

      Example URL for JSON output

      For json to dataframe help check out this stackoverflow query

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-01
        • 2020-10-29
        • 1970-01-01
        • 2020-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多