如何用AutoHotkey程序化获取谷歌搜索结果首条URL及视频链接解析
Extract the First Google Video Search Result URL with AutoHotkey
Great start with fetching the Google Video search page HTML! To pull the first video URL, we need to parse the HTML to target Google's redirect link (they wrap actual video URLs in their /url endpoint), then decode the encoded URL to get the real address. Here's how to extend your code:
Step 1: Update Your Existing Code
Add the parsing logic right after retrieving the HTML content:
Send ^c ; Use URL encoding for the clipboard content to handle special characters safely if !Html := UrlGet("https://www.google.com/search?tbm=vid&hl=en-TR&source=hp&biw=&bih=&q=" . EncodeURIComponent(Clipboard)) { MsgBox No data returned from Google.com return } ; Match the first Google redirect link pattern if !RegExMatch(Html, "i)/url\?q=([^&]+)", match) { MsgBox No video result found for your query return } ; Decode the URL-encoded video link to get the actual address firstVideoUrl := UrlDecode(match1) ; Example action: Open the video in your default browser Run, %firstVideoUrl% MsgBox First video URL: %firstVideoUrl%
Step 2: Add Required Helper Functions
AutoHotkey v1 doesn’t include built-in URL encoding/decoding, so include these functions to handle special characters:
; URL-encode text to safely pass query parameters EncodeURIComponent(str) { VarSetCapacity(encoded, StrLen(str)*3, 0) if DllCall("Urlmon\UrlEscape", "Str", str, "Str", encoded, "UInt", StrLen(str)*3, "UInt", 0) return encoded return str } ; URL-decode text to retrieve the actual video URL UrlDecode(str) { str := StrReplace(str, "+", " ") VarSetCapacity(decoded, StrLen(str)*2, 0) if DllCall("Urlmon\UrlUnescape", "Str", str, "Str", decoded, "UInt", StrLen(str)*2, "UInt", 0) return decoded return str }
Key Details to Keep in Mind:
- Regex Breakdown: The pattern
/url\?q=([^&]+)targets the first Google redirect link, capturing everything afterq=until the first&(which signals the start of extra Google parameters). - URL Safety: Using
EncodeURIComponenton the clipboard content ensures spaces, ampersands, and other special characters don’t break the search URL. - Google’s Dynamic HTML: Google often updates its search page structure. If this regex stops working, inspect the page source to find the new pattern for video links (look for anchor tags pointing to video content).
内容的提问来源于stack exchange,提问作者3code
相关产品推荐
相关产品推荐

