Sunday, June 23, 2024

Beautiful Soup Example codes 📋

Example 1: Extracting All Paragraphs from a Web Page 📄

```

url = 'http://example.com'

response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')

paragraphs = soup.find_all('p')

for p in paragraphs:

    print(p.get_text())

```

Example 2: Extracting Table Data 📊

```

url = 'http://example.com/tablepage'

response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')

table = soup.find('table')

rows = table.find_all('tr')

for row in rows:

    cells = row.find_all('td')

    for cell in cells:

        print(cell.get_text())

```

Example 3: Extracting Data from a Specific Class 🎯

```

url = 'http://example.com'

response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')

items = soup.find_all(class_='classname')

for item in items:

    print(item.get_text())

```

No comments:

AI's Impact on the IT Industry 2026