不使用Beautiful Soup提取本地HTML文件href后超链接方法咨询
Got it, let's figure out how to extract those http links without relying on Beautiful Soup! Since you’ve already read the HTML file content into a string, here are two straightforward approaches using loops and basic operations:
方法1:纯字符串遍历查找(无额外依赖)
This approach uses basic string searching and a loop to hunt down every href= occurrence, then extracts the linked URL if it starts with http.
# 假设你已经完成了这一步:读取HTML文件内容 html_content = open("your_file.html", "r").read() extracted_links = [] current_pos = 0 # 循环查找所有href=的位置 while True: # 找到下一个"href="的起始索引 href_start = html_content.find('href=', current_pos) if href_start == -1: break # 没有更多匹配,退出循环 # 跳过"href="本身,定位到引号的位置 quote_start = href_start + len('href=') # 处理双引号或单引号的情况 if html_content[quote_start] in ('"', "'"): quote_char = html_content[quote_start] # 找到闭合的引号 quote_end = html_content.find(quote_char, quote_start + 1) if quote_end == -1: # 找不到闭合引号,跳过这个无效的href current_pos = href_start + 1 continue # 提取引号内的内容并去除首尾空格 raw_link = html_content[quote_start + 1:quote_end].strip() # 只保留以http开头的链接 if raw_link.startswith('http'): extracted_links.append(raw_link) # 更新查找位置,避免重复匹配同一个href current_pos = href_start + 1 # 遍历输出结果(这里用了for循环,符合你的需求) print("Extracted links:") for link in extracted_links: print(link)
说明
- 这个方法完全不需要额外库,纯靠字符串操作和循环实现
- 能处理
href="http..."和href='http...'两种引号格式 - 会跳过没有闭合引号的无效
href项
方法2:正则表达式配合for循环(更简洁)
If you don't mind using Python's built-in re module, regex can make this task much cleaner, and we'll use a for loop to iterate through all matches:
import re # 同样假设你已经读取了HTML内容 html_content = open("your_file.html", "r").read() # 正则表达式匹配:href后跟引号,里面是http开头的内容 link_pattern = r'href=["\'](http[^"\']+)["\']' # 找到所有匹配项 match_iter = re.finditer(link_pattern, html_content) extracted_links = [] # 用for循环遍历每个匹配结果 for match in match_iter: extracted_links.append(match.group(1)) # 输出结果 print("Extracted links:") for link in extracted_links: print(link)
说明
- 正则表达式能更精准地匹配合法的
href格式,减少无效匹配 re.finditer()返回一个迭代器,我们用for循环逐个提取链接- 同样只保留以
http开头的链接,符合你的需求
内容的提问来源于stack exchange,提问作者Nick
相关产品推荐
相关产品推荐

