【问题标题】:Getting the date from JATS XML with BeautifulSoup使用 BeautifulSoup 从 JATS XML 获取日期
【发布时间】:2017-06-08 22:05:22
【问题描述】:

如何使用 BeautifulSoup 从 JATS XML 中提取日期(epub)?

<pub-date pub-type="epub">
<day>12</day>
<month>09</month>
<year>2011</year>
</pub-date>

→ 2011-09-12

<pub-date pub-type="collection">
<year>2011</year>
</pub-date>

应该被忽略。

【问题讨论】:

    标签: python xml-parsing beautifulsoup


    【解决方案1】:

    在您的示例中,pub-type 是 pub-date 的 属性,该属性的值是 "epub"。要以良好的标准化格式(例如 JATS XML)浏览文档树,您需要使用 lxml,作为 standaloneparser within BeautifulSoup

    这里有两个使用 lxml.etree 的函数,仅当属性为“epub”时才使用 xpath 解析候选日期字段。我特别基于 PLOS 的 JATS XML 格式,希望在这里适用。

    import datetime
    import lxml.etree as et
    
    def parse_article_date(date_element, date_format='%Y %m %d'):
        """
        For an article date element, convert XML fields to a datetime object
        :param date_format: string format used to convert to datetime object
        :return: datetime object based on XML date fields
        """
        day = ''
        month = ''
        year = ''
        for item in date_element.getchildren():
            if item.tag == 'day':
                day = item.text
            if item.tag == 'month':
                month = item.text
            if item.tag == 'year':
                year = item.text
        date = (year, month, day)
        string_date = ' '.join(date)
        date = datetime.datetime.strptime(string_date, date_format)
    
        return date
    
    def get_article_pubdate(article_file, tag_path_elements=None, string_=False):
        """
        For a local article file, get its date of publication
        :param article_file: the xml file for a single article
        :param tag_path_elements: xpath search results of the location in the article's XML tree
        :param string_: defaults to False. If True, returns a date string instead of datetime object
        :return: dict of date type mapped to datetime object for that article
        """
        pub_date = {}
        if tag_path_elements is None:
            tag_path_elements = ("/",
                                 "article",
                                 "front",
                                 "article-meta",
                                 "pub-date")
    
        article_tree = et.parse(article_file)
        article_root = article_tree.getroot()
        tag_location = '/'.join(tag_path_elements)
        pub_date_fields = article_root.xpath(tag_location)
        print(pub_date_fields)
    
        for element in pub_date_fields:
            pub_type = element.get('pub-type')
            if pub_type == 'epub':
                date = parse_article_date(element)
                pub_date[pub_type] = date
    
        if string_:
            for key, value in pub_date.items():
                if value:
                    pub_date[key] = value.strftime('%Y-%m-%d')  # you can set this to any date format
    
        return pub_date
    

    【讨论】:

    • 感谢您的辛勤工作。以this PLoS paper 为例:如何找到xpath 搜索结果?
    • 添加了一个打印语句,以便您在运行该函数时可以看到它。代表 xpath 搜索结果的项目是 pub_date_fields。如果您的问题是如何使用远程托管的 XML 执行此操作,您可以运行 get_article_pubdate(url),因为 etree 也可以解析远程文档。希望这会有所帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2018-10-28
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多