首页 > 解决方案 > 如何列出给定域中的唯一网址

问题描述

我已经编写了代码来从给定站点中提取所有 url,但问题是某些 url 重复,我希望它是一个具有唯一 url 的列表。

from bs4 import BeautifulSoup
from termcolor import colored
import re, os

import requests

url = 'http://example.com'
ext = 'html'
count=0
countfiles=0
files=[]
def ulist(x):
  return list(dict.fromkeys(x))



def listFD(filename, ext=''):
  print filename
  print url
  if filename == url:
      page = requests.get(url).text
  else:
      page = requests.get(url + filename).text

  soup = BeautifulSoup(page, 'html.parser')
  return ['/' + node.get('href') for node in soup.find_all('a') if node.get('href').endswith(ext)]



for file in ulist(listFD(url, ext)):
   for unfile in ulist(listFD(file, ext)):
    print unfile

标签: pythonlisturl

解决方案


您可以采取以下行动:

urls = list(set(urls))

推荐阅读