Hyper(link) Active
"web-crawling" के लिए निकालने वाले सबसे अहम attributes में से एक है a टैग के भीतर का hyperlink URL (href attribute). यहाँ, आप ऐसा ही एक hyperlink निकालेंगे! हमने print_attribute फंक्शन बनाया है जो आपके XPath से निकाले गए डेटा को प्रिंट करता है, ताकि आप चाहें तो कंसोल में अपने XPath स्ट्रिंग्स टेस्ट कर सकें.
यह अभ्यास निम्न HTML सोर्स कोड को संदर्भित करता है:
<html>
<body>
<div id="div1" class="class-1">
<p class="class-1 class-2">Hello World!</p>
<div id="div2">
<p id="p2" class="class-2">Choose
<a href="http://datacamp.com">DataCamp!</a>!
</p>
</div>
</div>
<div id="div3" class="class-2">
<p class="class-2">Thanks for Watching!</p>
</div>
</body>
</html>
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Web Scraping
अभ्यास निर्देश
- नीचे वैरिएबल
xpathको पूरा करने के लिए खाली स्थान भरें, ताकि DataCamp hyperlink सेhrefattribute value को select किया जा सके.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Create an xpath to the href attribute
xpath = '//p[@id=____]/a/____'
# Print out the selection(s); there should be only one
print_attribute( xpath )