出现的问题:

def get_all_ascii_art(self):
result_dic = {}
result_list = []
ascii_art_url = 'https://www.xxxx.net/example'
response = httpx.get(ascii_art_url, headers=self.headers_)
selector = Selector(response.text)
li_list = selector.xpath('//div[@class="category-list"]/ul/li').extract()
for li in li_list:
li = Selector(li)
# link = li.xpath('//a/@href').extract_first()
link = li.css('a::attr(href)').get()
url = ''.join(['https://www.xxxx.net', link])

result_dic['url'] = url
print(url)
type_name = li.css('a::text').get()
result_dic['name'] = type_name
print(type_name)
counts = li.css('small::text').get()
result_dic['count'] = counts
print(counts)
print(result_dic)
result_list.append(result_dic)
print(result_list)
return result_list
上述代码 想把字典result_dic的数据在循环中添加到列表result_list,
运行结果却是:

[{'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}]

Process finished with exit code 0

如上:每次for循环之后数据添加到字典中时都会覆盖掉上次添加的数据.

回头再去看看python中字典数据类型的使用方式。。。发现

使用字典的类似dict['a'] = b,这种赋值方式时:如果字典里有相对应的key,那么数据就会被覆盖掉;如果没有对应的key才会添加到字典里。

(引用:如果对 dict 中存在的 key-value 对赋值,新赋的 value 就会覆盖原有的 value,)

同理:update() 方法可使用一个字典所包含的 key-value 对来更新己有的字典。在执行 update() 方法时,如果被更新的字典中己包含对应的 key-value 对,

那么原 value 会被覆盖;如果被更新的字典中不包含对应的 key-value 对,则该 key-value 对被添加进去。

解决办法:

1.在循环里字典初始化,每次for循环都将字典初始化,再添加数据,就不会被覆盖。(每次字典重新开辟内存空间,并使变量dict指向该空间,因此不会出现相同地址的问题)

2.每次为列表添加数据的时候,在内存中其他位置创建与该字典相同的数据并加入列表(若字典内包含列表,需要使用deepcopy)

由于上述代码中使用的字典内没有其他容器型数据,所以使用第一种方法来解决数据被覆盖的问题

结果如下:

[{'url': 'https://www.xxxxl.net/ascii-art/chinese', 'name': '中文文字', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/animals', 'name': '动物', 'count': '(592)'}, {'url': 'https://www.xxxxl.net/ascii-art/vehicles', 'name': '车辆', 'count': '(109)'}, {'url': 'https://www.xxxxl.net/ascii-art/airplanes', 'name': '飞机', 'count': '(159)'}, {'url': 'https://www.xxxxl.net/ascii-art/weapons', 'name': '武器', 'count': '(88)'}, {'url': 'https://www.xxxxl.net/ascii-art/nature', 'name': '自然', 'count': '(96)'}, {'url': 'https://www.xxxxl.net/ascii-art/mythology', 'name': '神话', 'count': '(116)'}, {'url': 'https://www.xxxxl.net/ascii-art/space', 'name': '宇宙太空', 'count': '(75)'}, {'url': 'https://www.xxxxl.net/ascii-art/holiday-and-events', 'name': '节日与活动', 'count': '(96)'}, {'url': 'https://www.xxxxl.net/ascii-art/books', 'name': '书籍', 'count': '(27)'}, {'url': 'https://www.xxxxl.net/ascii-art/cartoons', 'name': '卡通动漫', 'count': '(273)'}, {'url': 'https://www.xxxxl.net/ascii-art/sports', 'name': '运动', 'count': '(101)'}, {'url': 'https://www.xxxxl.net/ascii-art/movies', 'name': '电影', 'count': '(126)'}, {'url': 'https://www.xxxxl.net/ascii-art/electronics', 'name': '电子设备', 'count': '(105)'}, {'url': 'https://www.xxxxl.net/ascii-art/computers', 'name': '计算机', 'count': '(111)'}, {'url': 'https://www.xxxxl.net/ascii-art/buildings', 'name': '建筑物', 'count': '(146)'}, {'url': 'https://www.xxxxl.net/ascii-art/people', 'name': '人物', 'count': '(168)'}, {'url': 'https://www.xxxxl.net/ascii-art/plants', 'name': '植物', 'count': '(115)'}, {'url': 'https://www.xxxxl.net/ascii-art/toys', 'name': '玩具', 'count': '(21)'}, {'url': 'https://www.xxxxl.net/ascii-art/religion', 'name': '宗教&信仰', 'count': '(22)'}, {'url': 'https://www.xxxxl.net/ascii-art/comic', 'name': '漫画', 'count': '(47)'}, {'url': 'https://www.xxxxl.net/ascii-art/food', 'name': '食物&饮料', 'count': '(57)'}, {'url': 'https://www.xxxxl.net/ascii-art/art-and-design', 'name': '艺术&设计', 'count': '(109)'}, {'url': 'https://www.xxxxl.net/ascii-art/music', 'name': '音乐', 'count': '(33)'}, {'url': 'https://www.xxxxl.net/ascii-art/clothing-and-accessories', 'name': '服装首饰', 'count': '(52)'}, {'url': 'https://www.xxxxl.net/ascii-art/furniture', 'name': '家具&装饰', 'count': '(87)'}, {'url': 'https://www.xxxxl.net/ascii-art/gesture', 'name': '手势', 'count': '(27)'}, {'url': 'https://www.xxxxl.net/ascii-art/characters', 'name': '英文文字', 'count': '(43)'}, {'url': 'https://www.xxxxl.net/ascii-art/games', 'name': '游戏', 'count': '(67)'}, {'url': 'https://www.xxxxl.net/ascii-art/logos', 'name': '图标', 'count': '(108)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}]

Process finished with exit code 0

 

posted on 2021-08-22 11:01  FilexHu  阅读(1418)  评论(0编辑  收藏  举报