위키백과 문서를 XML 파일로 저장

Jmnote (토론 | 기여)님의 2024년 3월 25일 (월) 14:06 판 (→‎예시 2)

1 개요

위키백과 문서를 XML 파일로 저장

2 예시 1

import requests

# API 요청을 보내어 데이터 가져오기
url = "https://en.wikipedia.org/w/api.php"
params = {
    "action": "query",
    "format": "xml",
    "titles": "Albert_Einstein",
    "prop": "revisions",
    "rvprop": "content"
}
response = requests.get(url, params=params)
data = response.content

# XML 데이터를 파일로 저장
with open("wiki_data.xml", "wb") as file:
    file.write(data)

# 파일 내용 첫부분 확인
with open("wiki_data.xml") as input_file:
    head = [next(input_file) for _ in range(10)]
print(head)


3 예시 2

import requests

url = "https://en.wikipedia.org/w/api.php"
params = {
    "action": "query",
    "export": 1,
    "format": "xml",
    "titles": "Albert_Einstein"
}
response = requests.get(url=url, params=params)
data = response.text
print(data)
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}