【发布时间】:2022-01-16 05:29:39
【问题描述】:
当我将列表转换为字典时,字典中只存储了一个元素,我不知道为什么。这是我的代码。我在网页抓取中使用 BeautifulSoup 并存储在列表中。最后,我想清理它们并将它们存储在 JSON 文件中
data = []
qa_dict={}
q_text=[]
q_username=[]
q_time=[]
r_text=[]
for i in range(1,2):
print(i)
url='https://drakhosravi.com/faq/?cat=all&hpage='+str(i)
response1 = requests.get(url).content.decode()
soup = BeautifulSoup(response1,'html.parser')
for s in soup.select('span.hamyar-comment-person-name') :
if(s.text!='دکتر آرزو خسروی'):
q_username.append(s.text.strip())
for times in soup.select('div.comment-header'):
for s in times.select('span.hamyar-comment-date') :
q_time.append(s.text.strip())
question=[]
for comment in soup.select('div.comment-body'):
question.append(comment.text.strip())
q_text=question[0::2]
for r in soup.select('ol.faq-comment_replies'):
for head in r.select('li'):
for t in head.select('div.comment-body'):
r_text.append(t.text.strip())
for username,qtime,qtxt,rtxt in zip(q_username,q_time,q_text,r_text):
qa_dict= {'username':username,'question_time':qtime,'question_text':qtxt,'url':url,'respond_text':rtxt,'responder_profile_url':'https://drakhosravi.com/about-us'}
data.append(qa_dict)
with open('drakhosravi2.json', 'w+', encoding="utf-8") as handle:
json.dump(qa_dict, handle, indent=4, ensure_ascii=False)
【问题讨论】:
-
您在每次
for迭代中重写qa_dict。创建一个字典列表并附加你的新字典
标签: python json beautifulsoup zip