जैसा कि अन्य ने कहा है, एक दृष्टिकोण विकीमीडिया एपीआई और urllib या urllib2 का उपयोग करना है। नीचे दिए गए कोड के टुकड़े उस हिस्से का हिस्सा हैं जो मैंने "लीड" खंड कहा जाता है, जिसमें आलेख सार और इन्फोबॉक्स है। यह जांच करेगा कि लौटाया गया पाठ वास्तविक सामग्री के बजाय रीडायरेक्ट है या नहीं, और अगर आप मौजूद हैं तो इन्फोबॉक्स को छोड़ने दें (मेरे मामले में मैंने इन्फोबॉक्स को खींचने और प्रारूपित करने के लिए अलग-अलग कोड का उपयोग किया था।
contentBaseURL='http://en.wikipedia.org/w/index.php?title='
def getContent(title):
URL=contentBaseURL+title+'&action=raw§ion=0'
f=urllib.urlopen(URL)
rawContent=f.read()
return rawContent
infoboxPresent = 0
# Check if a redirect was returned. If so, go to the redirection target
if rawContent.find('#REDIRECT') == 0:
rawContent = getFullContent(title)
# extract the redirection title
# Extract and format the Infobox
redirectStart=rawContent.find('#REDIRECT[[')+11
count = 0
redirectEnd = 0
for i, char in enumerate(rawContent[redirectStart:-1]):
if char == "[": count += 1
if char == "]}":
count -= 1
if count == 0:
redirectEnd = i+redirectStart+1
break
redirectTitle = rawContent[redirectStart:redirectEnd]
print 'redirectTitle is: ',redirectTitle
rawContent = getContent(redirectTitle)
# Skip the Infobox
infoboxStart=rawContent.find("{{Infobox") #Actually starts at the double {'s before "Infobox"
count = 0
infoboxEnd = 0
for i, char in enumerate(rawContent[infoboxStart:-1]):
if char == "{": count += 1
if char == "}":
count -= 1
if count == 0:
infoboxEnd = i+infoboxStart+1
break
if infoboxEnd <> 0:
rawContent = rawContent[infoboxEnd:]
आप, विकी मार्कअप सहित कच्चे पाठ वापस पाने रखा जाएगा ताकि आप कुछ को साफ करने की आवश्यकता होगी। यदि आप केवल पहले अनुच्छेद चाहते हैं, तो पूरे पहले खंड में नहीं, पहले नए पंक्ति चरित्र की तलाश करें।
पृष्ठ प्राप्त करने के लिए 'urllib' और HTML को पार्स करने के लिए' सुंदर सूप '। यद्यपि ऐसा करने के अन्य तरीके हैं, फिर भी उन्हें स्टैक ओवरव्लो पर खोजें। इस पर कई बार चर्चा की गई है। – user225312
आप किस मार्कअप में चाहते हैं? मीडियाविकी, एचटीएमएल? – khachik
एचटीएमएल। (15 वर्ण सीमा) –