Telegram Web
πŸ’‘ Top 70 Web Scraping Operations in Python

I. Making HTTP Requests (requests)

β€’ Import the library.
import requests

β€’ Make a GET request to a URL.
response = requests.get('http://example.com')

β€’ Check the response status code (200 is OK).
print(response.status_code)

β€’ Access the raw HTML content (as bytes).
html_bytes = response.content

β€’ Access the HTML content (as a string).
html_text = response.text

β€’ Access response headers.
print(response.headers)

β€’ Send a custom User-Agent header.
headers = {'User-Agent': 'My Cool Scraper 1.0'}
response = requests.get('http://example.com', headers=headers)

β€’ Pass URL parameters in a request.
params = {'q': 'python scraping'}
response = requests.get('https://www.google.com/search', params=params)

β€’ Make a POST request with form data.
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('http://httpbin.org/post', data=payload)

β€’ Handle potential request errors.
try:
response = requests.get('http://example.com', timeout=5)
response.raise_for_status() # Raise an exception for bad status codes
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")


II. Parsing HTML with BeautifulSoup (Setup & Navigation)

β€’ Import the library.
from bs4 import BeautifulSoup

β€’ Create a BeautifulSoup object from HTML text.
soup = BeautifulSoup(html_text, 'html.parser')

β€’ Prettify the parsed HTML for readability.
print(soup.prettify())

β€’ Access a tag directly by name (gets the first one).
title_tag = soup.title

β€’ Navigate to a tag's parent.
title_parent = soup.title.parent

β€’ Get an iterable of a tag's children.
for child in soup.head.children:
print(child.name)

β€’ Get the next sibling tag.
first_p = soup.find('p')
next_p = first_p.find_next_sibling('p')

β€’ Get the previous sibling tag.
second_p = soup.find_all('p')[1]
prev_p = second_p.find_previous_sibling('p')


III. Finding Elements with BeautifulSoup
β€’ Find the first occurrence of a tag.
first_link = soup.find('a')

β€’ Find all occurrences of a tag.
all_links = soup.find_all('a')

β€’ Find tags by their CSS class.
articles = soup.find_all('div', class_='article-content')

β€’ Find a tag by its ID.
main_content = soup.find(id='main-container')

β€’ Find tags by other attributes.
images = soup.find_all('img', attrs={'data-src': True})

β€’ Find using a list of multiple tags.
headings = soup.find_all(['h1', 'h2', 'h3'])

β€’ Find using a regular expression.
import re
links_with_blog = soup.find_all('a', href=re.compile(r'blog'))

β€’ Find using a custom function.
# Finds tags with a 'class' but no 'id'
tags = soup.find_all(lambda tag: tag.has_attr('class') and not tag.has_attr('id'))

β€’ Limit the number of results.
first_five_links = soup.find_all('a', limit=5)

β€’ Use CSS Selectors to find one element.
footer = soup.select_one('#footer > p')

β€’ Use CSS Selectors to find all matching elements.
article_links = soup.select('div.article a')

β€’ Select direct children using CSS selector.
nav_items = soup.select('ul.nav > li')


IV. Extracting Data with BeautifulSoup

β€’ Get the text content from a tag.
title_text = soup.title.get_text()

β€’ Get stripped text content.
link_text = soup.find('a').get_text(strip=True)

β€’ Get all text from the entire document.
all_text = soup.get_text()

β€’ Get an attribute's value (like a URL).
link_url = soup.find('a')['href']

β€’ Get the tag's name.
tag_name = soup.find('h1').name

β€’ Get all attributes of a tag as a dictionary.
attrs_dict = soup.find('img').attrs


V. Parsing with lxml and XPath

β€’ Import the library.
from lxml import html

β€’ Parse HTML content with lxml.
tree = html.fromstring(response.content)

β€’ Select elements using an XPath expression.
# Selects all <a> tags inside <div> tags with class 'nav'
links = tree.xpath('//div[@class="nav"]/a')

β€’ Select text content directly with XPath.
# Gets the text of all <h1> tags
h1_texts = tree.xpath('//h1/text()')

β€’ Select an attribute value with XPath.
# Gets all href attributes from <a> tags
hrefs = tree.xpath('//a/@href')


VI. Handling Dynamic Content (Selenium)

β€’ Import the webdriver.
from selenium import webdriver

β€’ Initialize a browser driver.
driver = webdriver.Chrome() # Requires chromedriver

β€’ Navigate to a webpage.
driver.get('http://example.com')

β€’ Find an element by its ID.
element = driver.find_element('id', 'my-element-id')

β€’ Find elements by CSS Selector.
elements = driver.find_elements('css selector', 'div.item')

β€’ Find an element by XPath.
button = driver.find_element('xpath', '//button[@type="submit"]')

β€’ Click a button.
button.click()

β€’ Enter text into an input field.
search_box = driver.find_element('name', 'q')
search_box.send_keys('Python Selenium')

β€’ Wait for an element to become visible.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)

β€’ Get the page source after JavaScript has executed.
dynamic_html = driver.page_source

β€’ Close the browser window.
driver.quit()


VII. Common Tasks & Best Practices

β€’ Handle pagination by finding the "Next" link.
next_page_url = soup.find('a', text='Next')['href']

β€’ Save data to a CSV file.
import csv
with open('data.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['Title', 'Link'])
# writer.writerow([title, url]) in a loop

β€’ Save data to CSV using pandas.
import pandas as pd
df = pd.DataFrame(data, columns=['Title', 'Link'])
df.to_csv('data.csv', index=False)

β€’ Use a proxy with requests.
proxies = {'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080'}
requests.get('http://example.com', proxies=proxies)

β€’ Pause between requests to be polite.
import time
time.sleep(2) # Pause for 2 seconds

β€’ Handle JSON data from an API.
json_response = requests.get('https://api.example.com/data').json()

β€’ Download a file (like an image).
img_url = 'http://example.com/image.jpg'
img_data = requests.get(img_url).content
with open('image.jpg', 'wb') as handler:
handler.write(img_data)

β€’ Parse a sitemap.xml to find all URLs.
# Get the sitemap.xml file and parse it like any other XML/HTML to extract <loc> tags.


VIII. Advanced Frameworks (Scrapy)

β€’ Create a Scrapy spider (conceptual command).
scrapy genspider example example.com

β€’ Define a parse method to process the response.
# In your spider class:
def parse(self, response):
# parsing logic here
pass

β€’ Extract data using Scrapy's CSS selectors.
titles = response.css('h1::text').getall()

β€’ Extract data using Scrapy's XPath selectors.
links = response.xpath('//a/@href').getall()

β€’ Yield a dictionary of scraped data.
yield {'title': response.css('title::text').get()}

β€’ Follow a link to parse the next page.
next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
yield response.follow(next_page, callback=self.parse)

β€’ Run a spider from the command line.
scrapy crawl example -o output.json

β€’ Pass arguments to a spider.
scrapy crawl example -a category=books

β€’ Create a Scrapy Item for structured data.
import scrapy
class ProductItem(scrapy.Item):
name = scrapy.Field()
price = scrapy.Field()

β€’ Use an Item Loader to populate Items.
from scrapy.loader import ItemLoader
loader = ItemLoader(item=ProductItem(), response=response)
loader.add_css('name', 'h1.product-name::text')


#Python #WebScraping #BeautifulSoup #Selenium #Requests

━━━━━━━━━━━━━━━
By: @DataScienceN ✨
❀3
πŸ”₯ Trending Repository: nocobase

πŸ“ Description: NocoBase is the most extensible AI-powered no-code/low-code platform for building business applications and enterprise solutions.

πŸ”— Repository URL: https://github.com/nocobase/nocobase

🌐 Website: https://www.nocobase.com

πŸ“– Readme: https://github.com/nocobase/nocobase#readme

πŸ“Š Statistics:
🌟 Stars: 17.7K stars
πŸ‘€ Watchers: 147
🍴 Forks: 2K forks

πŸ’» Programming Languages: TypeScript - JavaScript - Smarty - Shell - Dockerfile - Less

🏷️ Related Topics:
#internal_tools #crud #crm #admin_dashboard #self_hosted #web_application #project_management #salesforce #developer_tools #airtable #workflows #low_code #no_code #app_builder #internal_tool #nocode #low_code_development_platform #no_code_platform #low_code_platform #low_code_framework


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: alertmanager

πŸ“ Description: Prometheus Alertmanager

πŸ”— Repository URL: https://github.com/prometheus/alertmanager

🌐 Website: https://prometheus.io

πŸ“– Readme: https://github.com/prometheus/alertmanager#readme

πŸ“Š Statistics:
🌟 Stars: 7.3K stars
πŸ‘€ Watchers: 166
🍴 Forks: 2.3K forks

πŸ’» Programming Languages: Go - Elm - HTML - Makefile - TypeScript - JavaScript

🏷️ Related Topics:
#notifications #slack #monitoring #email #pagerduty #alertmanager #hacktoberfest #deduplication #opsgenie


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: gopeed

πŸ“ Description: A modern download manager that supports all platforms. Built with Golang and Flutter.

πŸ”— Repository URL: https://github.com/GopeedLab/gopeed

🌐 Website: https://gopeed.com

πŸ“– Readme: https://github.com/GopeedLab/gopeed#readme

πŸ“Š Statistics:
🌟 Stars: 21K stars
πŸ‘€ Watchers: 167
🍴 Forks: 1.5K forks

πŸ’» Programming Languages: Dart - Go - C++ - CMake - Swift - Ruby

🏷️ Related Topics:
#android #windows #macos #golang #http #ios #torrent #downloader #debian #bittorrent #cross_platform #ubuntu #https #flutter #magnet


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: vertex-ai-creative-studio

πŸ“ Description: GenMedia Creative Studio is a Vertex AI generative media user experience highlighting the use of Imagen, Veo, Gemini 🍌, Gemini TTS, Chirp 3, Lyria and other generative media APIs on Google Cloud.

πŸ”— Repository URL: https://github.com/GoogleCloudPlatform/vertex-ai-creative-studio

πŸ“– Readme: https://github.com/GoogleCloudPlatform/vertex-ai-creative-studio#readme

πŸ“Š Statistics:
🌟 Stars: 512 stars
πŸ‘€ Watchers: 19
🍴 Forks: 200 forks

πŸ’» Programming Languages: Jupyter Notebook - Python - TypeScript - Go - JavaScript - Shell

🏷️ Related Topics:
#google_cloud #gemini #chirp #imagen #veo #lyria #vertex_ai #nano_banana


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: Parabolic

πŸ“ Description: Download web video and audio

πŸ”— Repository URL: https://github.com/NickvisionApps/Parabolic

🌐 Website: https://flathub.org/apps/details/org.nickvision.tubeconverter

πŸ“– Readme: https://github.com/NickvisionApps/Parabolic#readme

πŸ“Š Statistics:
🌟 Stars: 4.1K stars
πŸ‘€ Watchers: 28
🍴 Forks: 188 forks

πŸ’» Programming Languages: C++ - CMake - Python - Inno Setup - C - CSS

🏷️ Related Topics:
#music #windows #downloader #youtube #qt #cpp #youtube_dl #gnome #videos #flathub #gtk4 #yt_dlp #libadwaita


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: localstack

πŸ“ Description: πŸ’» A fully functional local AWS cloud stack. Develop and test your cloud & Serverless apps offline

πŸ”— Repository URL: https://github.com/localstack/localstack

🌐 Website: https://localstack.cloud

πŸ“– Readme: https://github.com/localstack/localstack#readme

πŸ“Š Statistics:
🌟 Stars: 61.1K stars
πŸ‘€ Watchers: 514
🍴 Forks: 4.3K forks

πŸ’» Programming Languages: Python - Shell - Makefile - ANTLR - JavaScript - Java

🏷️ Related Topics:
#python #testing #aws #cloud #continuous_integration #developer_tools #localstack


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: go-sdk

πŸ“ Description: The official Go SDK for Model Context Protocol servers and clients. Maintained in collaboration with Google.

πŸ”— Repository URL: https://github.com/modelcontextprotocol/go-sdk

πŸ“– Readme: https://github.com/modelcontextprotocol/go-sdk#readme

πŸ“Š Statistics:
🌟 Stars: 2.7K stars
πŸ‘€ Watchers: 39
🍴 Forks: 249 forks

πŸ’» Programming Languages: Go

🏷️ Related Topics: Not available

==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: rachoon

πŸ“ Description: 🦝 Rachoon β€” A self-hostable way to handle invoices

πŸ”— Repository URL: https://github.com/ad-on-is/rachoon

πŸ“– Readme: https://github.com/ad-on-is/rachoon#readme

πŸ“Š Statistics:
🌟 Stars: 292 stars
πŸ‘€ Watchers: 4
🍴 Forks: 14 forks

πŸ’» Programming Languages: TypeScript - Vue - HTML - SCSS - Dockerfile - JavaScript - Shell

🏷️ Related Topics: Not available

==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: Kotatsu

πŸ“ Description: Manga reader for Android

πŸ”— Repository URL: https://github.com/KotatsuApp/Kotatsu

🌐 Website: https://kotatsu.app

πŸ“– Readme: https://github.com/KotatsuApp/Kotatsu#readme

πŸ“Š Statistics:
🌟 Stars: 7.2K stars
πŸ‘€ Watchers: 72
🍴 Forks: 366 forks

πŸ’» Programming Languages: Kotlin

🏷️ Related Topics:
#android #manga #comics #mangareader #manga_reader #webtoon


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: ggml

πŸ“ Description: Tensor library for machine learning

πŸ”— Repository URL: https://github.com/ggml-org/ggml

πŸ“– Readme: https://github.com/ggml-org/ggml#readme

πŸ“Š Statistics:
🌟 Stars: 13.4K stars
πŸ‘€ Watchers: 141
🍴 Forks: 1.4K forks

πŸ’» Programming Languages: C++ - C - Cuda - Metal - GLSL - CMake

🏷️ Related Topics:
#machine_learning #automatic_differentiation #tensor_algebra #large_language_models


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: asm-lessons

πŸ“ Description: FFMPEG Assembly Language Lessons

πŸ”— Repository URL: https://github.com/FFmpeg/asm-lessons

πŸ“– Readme: https://github.com/FFmpeg/asm-lessons#readme

πŸ“Š Statistics:
🌟 Stars: 9.7K stars
πŸ‘€ Watchers: 153
🍴 Forks: 288 forks

πŸ’» Programming Languages: Not available

🏷️ Related Topics: Not available

==================================
🧠 By: https://www.tgoop.com/DataScienceM
❀1
πŸ”₯ Trending Repository: lima

πŸ“ Description: Linux virtual machines, with a focus on running containers

πŸ”— Repository URL: https://github.com/lima-vm/lima

🌐 Website: https://lima-vm.io/

πŸ“– Readme: https://github.com/lima-vm/lima#readme

πŸ“Š Statistics:
🌟 Stars: 18.4K stars
πŸ‘€ Watchers: 83
🍴 Forks: 722 forks

πŸ’» Programming Languages: Go - Shell - Makefile - Perl - HTML - SCSS

🏷️ Related Topics:
#macos #vm #qemu #containerd


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: mcp

πŸ“ Description: AWS MCP Servers β€” helping you get the most out of AWS, wherever you use MCP.

πŸ”— Repository URL: https://github.com/awslabs/mcp

🌐 Website: https://awslabs.github.io/mcp/

πŸ“– Readme: https://github.com/awslabs/mcp#readme

πŸ“Š Statistics:
🌟 Stars: 7K stars
πŸ‘€ Watchers: 68
🍴 Forks: 1K forks

πŸ’» Programming Languages: Python - Shell - Dockerfile - HTML - TypeScript - Jinja

🏷️ Related Topics:
#aws #mcp #mcp_servers #mcp_server #modelcontextprotocol #mcp_client #mcp_tools #mcp_host #mcp_clients


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: strix

πŸ“ Description: ✨ Open-source AI hackers for your apps πŸ‘¨πŸ»β€πŸ’»

πŸ”— Repository URL: https://github.com/usestrix/strix

🌐 Website: https://usestrix.com/

πŸ“– Readme: https://github.com/usestrix/strix#readme

πŸ“Š Statistics:
🌟 Stars: 3K stars
πŸ‘€ Watchers: 38
🍴 Forks: 394 forks

πŸ’» Programming Languages: Python - Jinja - Dockerfile

🏷️ Related Topics:
#artificial_intelligence #cybersecurity #penetration_testing #agents #llm #generative_ai


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: frigate

πŸ“ Description: NVR with realtime local object detection for IP cameras

πŸ”— Repository URL: https://github.com/blakeblackshear/frigate

🌐 Website: https://frigate.video

πŸ“– Readme: https://github.com/blakeblackshear/frigate#readme

πŸ“Š Statistics:
🌟 Stars: 26.8K stars
πŸ‘€ Watchers: 218
🍴 Forks: 2.5K forks

πŸ’» Programming Languages: TypeScript - Python - CSS - Shell - Dockerfile - JavaScript

🏷️ Related Topics:
#home_automation #mqtt #ai #camera #rtsp #tensorflow #nvr #realtime #home_assistant #homeautomation #object_detection #google_coral


==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: gumroad

πŸ“ Description: Sell stuff and see what sticks

πŸ”— Repository URL: https://github.com/antiwork/gumroad

🌐 Website: https://gumroad.com

πŸ“– Readme: https://github.com/antiwork/gumroad#readme

πŸ“Š Statistics:
🌟 Stars: 7.4K stars
πŸ‘€ Watchers: 50
🍴 Forks: 1.4K forks

πŸ’» Programming Languages: Ruby - TypeScript - HTML - SCSS - Shell - JavaScript

🏷️ Related Topics: Not available

==================================
🧠 By: https://www.tgoop.com/DataScienceM
πŸ”₯ Trending Repository: code-server

πŸ“ Description: VS Code in the browser

πŸ”— Repository URL: https://github.com/coder/code-server

🌐 Website: https://coder.com

πŸ“– Readme: https://github.com/coder/code-server#readme

πŸ“Š Statistics:
🌟 Stars: 74.6K stars
πŸ‘€ Watchers: 734
🍴 Forks: 6.3K forks

πŸ’» Programming Languages: TypeScript - Shell - HTML - CSS - HCL - JavaScript

🏷️ Related Topics:
#ide #vscode #development_environment #remote_work #dev_tools #browser_ide #vscode_remote


==================================
🧠 By: https://www.tgoop.com/DataScienceM
2025/11/18 02:51:25
Back to Top
HTML Embed Code: