Response से खुलासा
हमने एक Response object, जिसका नाम response है, पहले से लोड कर रखा है जिसमें एक सीक्रेट वेबसाइट की content है. आपका काम है response वैरिएबल का उपयोग करके उस वेबसाइट का URL और title पता करना. पिछली lesson में आपने URL ढूँढना सीखा था. वेबसाइट का title ढूँढने के लिए आपको यह जानना होगा:
- टाइटल,
titleelement का text होता है titleelement,headelement का child है, औरheadelement,htmlroot element का child है.
ध्यान दें: html root element का केवल एक child head element होता है, और head element का केवल एक child title element होता है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Web Scraping
अभ्यास निर्देश
- वैरिएबल
this_urlमें वह URL assign करें जिससेresponseवैरिएबल लोड किया गया था. - वैरिएबल
this_titleमें उस वेबसाइट का title assign करें जिससेresponseवैरिएबल लोड किया गया था. चूँकि हमें चुने गए एकल element से सिर्फ टेक्स्ट चाहिए, इसलिए टेक्स्ट निकालने के लिएextract_first()method का उपयोग करें. - चाहे आप
xpathइस्तेमाल करें याcss, सुनिश्चित करें कि आप title element के भीतर का text select कर रहे हैं, सिर्फ element नहीं.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Get the URL to the website loaded in response
this_url = ____
# Get the title of the website loaded in response
this_title = response.____.extract_first()
# Print out our findings
print_url_title( this_url, this_title )