使用BeautifulSoup将上一个结果保存到每个值的字典

2024-04-25 22:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用requestsBeautifulSoup制作一个网络爬虫。我使用for循环创建一个字典列表,其值是a标记的href。但是,我在做这件事时遇到了一些问题,因为所有的结果都将是该页的最后一个href。以下是我打印最终结果时的输出:

[{'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}, {'link': '/terms'}]

我不确定为什么它只做最后一个值。我假设这是因为在最后一个循环中,它将具有相同名称的所有键分配给该值。我怎么能到处修理这个?这是密码。你知道吗

import json
import requests
from bs4 import BeautifulSoup

tags_dict = {}
tags_list = []

r = requests.get("http://chicosadventures.com/")

soup = BeautifulSoup(r.content, "lxml")


for link in soup.find_all('a'):
    tags_dict['link'] = link.get('href')
    tags_list.append(tags_dict)

dump = json.dumps(tags_list)
print(dump)

Tags: import网络jsonforgettagslinkrequests
1条回答
网友
1楼 · 发布于 2024-04-25 22:30:30

你的问题是tags_dict。您只是在列表中一次又一次地存储对一个字典的引用,由于它是一个引用,最后一个值会反映在所有条目中。我修改了它,为每个迭代创建一个新的dict对象,现在它可以正常工作了

import json
import requests
from bs4 import BeautifulSoup

tags_list = []
r = requests.get("http://chicosadventures.com/")
soup = BeautifulSoup(r.content, "lxml")

for link in soup.find_all('a'):
    tags_list.append({"link": link.get('href')})

dump = json.dumps(tags_list)
print(dump)

输出:

[{"link": "/"}, {"link": "/about_chico"}, {"link": "/about_the_author"}, {"link": "/about_the_illustrator"}, {"link": "/chico_in_the_news_"}, {"link": "/order_your_copy"}, {"link": "/contact_us"}, {"link": "/about_chico"}, {"link": "/about_the_author"}, {"link": "/about_the_illustrator"}, {"link": "/chico_in_the_news_"}, {"link": "/order_your_copy"}, {"link": "/contact_us"}, {"link": "/privacy"}, {"link": "javascript:print()"}, {"link": "http://www.ebtech.net/"}, {"link": "/terms"}]

相关问题 更多 >