67 lines
3.1 KiB
Markdown
67 lines
3.1 KiB
Markdown
The most effective way to find a YouTube Channel ID from a channel handle (`@tavakfi`) using **Python without an API** is by web scraping the channel's HTML source code.
|
|
|
|
The Channel ID (which starts with `UC`) is embedded in the page source under a key like `"externalId"` or `"channelId"`.
|
|
|
|
Here is the Python script you can use:
|
|
|
|
### Python Channel ID Finder (No API)
|
|
|
|
This script uses the `requests` library to fetch the HTML content and the built-in `re` (regular expressions) library to search for the Channel ID.
|
|
|
|
```python
|
|
import requests
|
|
import re
|
|
|
|
def get_channel_id_from_handle(handle_url):
|
|
"""
|
|
Fetches the YouTube channel ID from a handle URL by scraping the source code.
|
|
"""
|
|
try:
|
|
# 1. Fetch the HTML content of the channel page
|
|
response = requests.get(handle_url)
|
|
response.raise_for_status() # Raise an exception for bad status codes
|
|
|
|
html_content = response.text
|
|
|
|
# 2. Search for the Channel ID using a Regular Expression
|
|
# The channel ID is often stored in the page data as "externalId" or "channelId"
|
|
# The regex looks for "externalId" or "channelId" followed by the UC... string
|
|
match = re.search(r'"externalId":"(UC[a-zA-Z0-9_-]{22})"', html_content)
|
|
|
|
if match:
|
|
# The Channel ID is captured in the first group of the regex match
|
|
return match.group(1)
|
|
else:
|
|
# Try alternative location for Channel ID (less common but worth checking)
|
|
match_alt = re.search(r'"channelId":"(UC[a-zA-Z0-9_-]{22})"', html_content)
|
|
if match_alt:
|
|
return match_alt.group(1)
|
|
else:
|
|
return "Channel ID not found in page source."
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
return f"Error fetching URL: {e}"
|
|
|
|
# The handle URL you provided
|
|
channel_url = "https://www.youtube.com/@tavakfi"
|
|
|
|
# Run the function
|
|
channel_id = get_channel_id_from_handle(channel_url)
|
|
|
|
print(f"Channel Handle: {channel_url}")
|
|
print(f"Channel ID: {channel_id}")
|
|
```
|
|
|
|
-----
|
|
|
|
### How the Code Works
|
|
|
|
1. **`import requests` and `import re`**: These lines import the necessary libraries. `requests` handles fetching the webpage content, and `re` handles finding the specific text pattern (the Channel ID) within that content.
|
|
2. **`requests.get(handle_url)`**: This line sends an HTTP GET request to the YouTube handle URL (`@tavakfi`) to retrieve the raw HTML source code.
|
|
3. **`re.search(r'"externalId":"(UC[a-zA-Z0-9_-]{22})"', html_content)`**: This is the core scraping logic. It searches the entire HTML text for the pattern that YouTube uses to store the Channel ID.
|
|
* The Channel ID always starts with **`UC`** and is followed by exactly 22 alphanumeric characters, which is captured by the pattern `(UC[a-zA-Z0-9_-]{22})`.
|
|
* The ID is then extracted using `match.group(1)`.
|
|
|
|
This scraping technique allows you to reliably retrieve the unique `UC` Channel ID for any channel that uses the modern `@handle` URL format.
|
|
|
|
You can learn more about the channel and its content by watching this video: [Türkiye Research Foundation - Videos](https://www.youtube.com/@tavakfi/videos). |