समस्या
आपको एक वेब पेज से HTML टेबल्स निकालने की आवश्यकता है।
परिचय
इंटरनेट और वर्ल्ड वाइड वेब (WWW) आज सूचना का सबसे प्रमुख स्रोत है। वहां इतनी जानकारी है कि इतने विकल्पों में से सामग्री चुनना बहुत कठिन है। उस जानकारी का अधिकांश हिस्सा HTTP के माध्यम से पुनर्प्राप्त करने योग्य है।
लेकिन हम इन ऑपरेशन्स को प्रोग्रामेटिक रूप से भी कर सकते हैं ताकि जानकारी को स्वचालित रूप से पुनर्प्राप्त और प्रोसेस किया जा सके।
पायथन हमें अपने स्टैंडर्ड लाइब्रेरी HTTP क्लाइंट का उपयोग करके ऐसा करने की अनुमति देता है, लेकिन requests मॉड्यूल वेब पेज की जानकारी प्राप्त करने को बहुत आसान बनाता है।
इस पोस्ट में, हम देखेंगे कि HTML पेजों को पार्स करके उनमें एम्बेडेड HTML टेबल्स को कैसे निकाला जाए।
कैसे करें
1. हम requests, pandas, beautifulsoup4 और tabulate पैकेज का उपयोग करेंगे। कृपया उन्हें अपने सिस्टम पर इंस्टॉल करें अगर वे गायब हैं।
import requests
import pandas as pd
from tabulate import tabulate
2. हम https://www.tutorialspoint.com/python/python_basic_operators.htm का उपयोग पेज को पार्स करने और उसके अंदर एम्बेडेड सभी HTML टेबल्स को प्रिंट करने के लिए करेंगे।
site_url = "https://www.tutorialspoint.com/python/python_basic_operators.htm"
3. सर्वर पर रिक्वेस्ट करें और रिस्पॉन्स देखें
response = requests.get(site_url)
print(f"*** The response for {site_url} is {response.status_code}")
4. रिक्वेस्ट हेडर्स, रिस्पॉन्स हेडर्स और सर्वर द्वारा लौटाए गए पहले 100 टेक्स्ट देखें
print(f"*** Printing the request headers - \n {response.request.headers}")
print(f"*** Printing the request headers - \n {response.headers}")
print(f"*** Accessing the first 100/{len(response.text)} characters - \n\n {response.text[:100]}")
5. BeautifulSoup का उपयोग करके HTML पेजों को पार्स करें
from bs4 import BeautifulSoup
tutorialpoints_page = BeautifulSoup(response.text, 'html.parser')
print(f"*** The title of the page is - {tutorialpoints_page.title}")
print(f"*** The title of the page is - {tutorialpoints_page.title.string}")
6. अधिकांश टेबल्स में हेडिंग h2, h3, h4, h5 या h6 टैग्स में परिभाषित होती हैं। हम पहले इन टैग्स की पहचान करते हैं फिर पहचाने गए टैग के बगल में HTML टेबल उठाते हैं। इस लॉजिक के लिए, हम find, sibling और find_next_siblings का उपयोग करेंगे।
tags = tutorialpoints_page.find(lambda elm: elm.name == "h2" or elm.name == "h3" or elm.name == "h4" or elm.name == "h5" or elm.name == "h6")
for sibling in tags.find_next_siblings():
if sibling.name == "table":
my_table = sibling
df = pd.read_html(str(my_table))
print(tabulate(df[0], headers='keys', tablefmt='psql'))
पूरा कोड एक साथ
import requests
import pandas as pd
from bs4 import BeautifulSoup
from tabulate import tabulate
site_url = "https://www.tutorialspoint.com/python/python_basic_operators.htm"
response = requests.get(site_url)
print(f"*** The response for {site_url} is {response.status_code}")
print(f"*** Printing the request headers - \n {response.request.headers}")
print(f"*** Printing the request headers - \n {response.headers}")
print(f"*** Accessing the first 100/{len(response.text)} characters - \n\n {response.text[:100]}")
tutorialpoints_page = BeautifulSoup(response.text, 'html.parser')
print(f"*** The title of the page is - {tutorialpoints_page.title}")
print(f"*** The title of the page is - {tutorialpoints_page.title.string}")
tags = tutorialpoints_page.find(lambda elm: elm.name == "h2" or elm.name == "h3" or elm.name == "h4" or elm.name == "h5" or elm.name == "h6")
for sibling in tags.find_next_siblings():
if sibling.name == "table":
my_table = sibling
df = pd.read_html(str(my_table))
print(tabulate(df[0], headers='keys', tablefmt='psql'))