Python 属性错误:';设置';对象没有属性';左';
我正在尝试创建一个站点地图生成器。简而言之,我给它一个链接,它在网站上寻找更多的链接等等 为了避免任何长链,我想我应该创建一个Python 属性错误:';设置';对象没有属性';左';,python,Python,我正在尝试创建一个站点地图生成器。简而言之,我给它一个链接,它在网站上寻找更多的链接等等 为了避免任何长链,我想我应该创建一个blocked_sites.txt,我可以从中读取并比较我的未处理的\u URL,并删除包含阻止程序的所有项目 我的问题是,由于天真,我认为我可以简单地进行一些集合/列表比较和删除,而viola则完成了,但是问题更大,主要是集合*deque* 代码 我首先定义我的分层url,这是用户输入,并将其添加到que: # a queue of urls to be crawle
blocked_sites.txt
,我可以从中读取并比较我的未处理的\u URL
,并删除包含阻止程序的所有项目
我的问题是,由于天真,我认为我可以简单地进行一些集合/列表比较和删除,而viola则完成了,但是问题更大,主要是集合*deque*
代码
我首先定义我的分层url,这是用户输入,并将其添加到que:
# a queue of urls to be crawled
unprocessed_urls = deque([starting_url])
现在,我将开始处理我的URL:
# process urls one by one from unprocessed_url queue until queue is empty
while len(unprocessed_urls):
# Remove unwanted items
unprocessed_urls = {url for url in unprocessed_urls if not any(blocker in url for blocker in blockers)} <-- THIS IS THE PROBLEM
# move next url from the queue to the set of processed urls
newurl = unprocessed_urls.popleft()
processed_urls.add(newurl)
# extract base url to resolve relative links
parts = urlsplit(newurl)
base_url = "{0.scheme}://{0.netloc}".format(parts)
if parts.scheme !='mailto' and parts.scheme !='#':
path = newurl[:newurl.rfind('/')+1] if '/' in parts.path else newurl
else:
continue
# get url's content
print(Fore.CYAN + "Crawling URL %s" % newurl + Fore.WHITE)
try:
response = requests.get(newurl, timeout=3)
通过使用建议的方法从不需要的URL中剥离未处理的URL
,我使用了这一行(代码中也指出了这一点):
因此,我们发现自己在这里:
AttributeError: 'set' object has no attribute 'popleft'
我能从中得出的结论是,通过尝试删除不需要的项目,它以某种方式改变了集合的类型
我真的不知道如何从这里向前移动。行未处理的\u URL={…}
创建一个新的集
对象,并将其分配给未处理的\u URL
。这个新值在逻辑上与旧值相似的事实是无关紧要的;赋值给一个变量会覆盖以前的内容
但是,可以从任何iterable创建collections.deque
,因此您可以这样做
unprocessed_urls = deque(url for url in unprocessed_urls if ...)
创建新的collections.deque
,以便分配给未处理URL的所有值将具有相同的类型
AttributeError: 'set' object has no attribute 'popleft'
unprocessed_urls = deque(url for url in unprocessed_urls if ...)