python - 如何列出给定域中的唯一网址
问题描述
我已经编写了代码来从给定站点中提取所有 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
解决方案
您可以采取以下行动:
urls = list(set(urls))
推荐阅读
- azure-ad-b2c - 无法翻译“验证码已发送,请复制到下方输入框。” 信息
- scala - Scala Future flatMap 实现(链接)
- c# - 如何使组合框选择更改在 C# 中进行计算
- python - 如果字符串由特定字母组成,则根据该过滤数据框
- http-headers - 内容安全策略阻止白名单域
- python-3.x - 尝试在 Python3 中使用中序遍历来验证二叉搜索树。我的方法有什么问题?
- python - 平滑地上下移动火球 Pygame
- python - 带有 Azure AD SSO 的 Snowflake python 连接器
- postgresql - 如何将 IAM 服务帐号连接到 Cloud SQL 实例
- python - 张量与向量列表的快速元素乘法