以下是一个BFS的Python 3实现示例,使用queue模块中的队列作为数据结构来存储节点和访问状态。可以通过对其进行适当改进来减少运行时间:
import urllib.request
import queue
start_url = "https://en.wikipedia.org/wiki/Breadth-first_search"
target_url = "https://en.wikipedia.org/wiki/Python_(programming_language)"
def extract_urls(url):
ext_url = []
with urllib.request.urlopen(url) as response:
html = response.read().decode()
start_link = html.find("