"위키백과 문서를 XML 파일로 저장"의 두 판 사이의 차이

2번째 줄: 2번째 줄:
;위키백과 문서를 XML 파일로 저장
;위키백과 문서를 XML 파일로 저장


==예시 1==
<syntaxhighlight lang='python' run>
<syntaxhighlight lang='python' run>
import requests
import requests
25번째 줄: 26번째 줄:
     head = [next(input_file) for _ in range(10)]
     head = [next(input_file) for _ in range(10)]
print(head)
print(head)
</syntaxhighlight>
==예시 2==
<syntaxhighlight lang='python' run>
import requests
url = "https://en.wikipedia.org/w/api.php"
params = {
    "action": "query",
    "export": 1,
    "format": "xml",
    "titles": "Albert_Einstein"
}
response = requests.get(url=wiki_api_url, params=params)
data = response.text
print(data)
</syntaxhighlight>
</syntaxhighlight>

2024년 3월 25일 (월) 14:06 판

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=wiki_api_url, params=params)
data = response.text
print(data)
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}