#解析数据 detail_selector = parsel.Selector(detail_html) title = detail_selector.css('.b_crumb_cont *:nth-child(3)::text').get() comment = detail_selector.css('.title white::text').get() view_count = detail_selector.css('.view_count::text').get() date = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.when > p > span.data::text').get() days = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.howlong > p > span.data::text').get() money = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.howmuch > p > span.data::text').get() character = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.who > p > span.data::text').get() play_list = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.how > p > span.data > span::text').getall() for play in play_list: print(play) print(title, comment, view_count, date, days, money, character, play_list, detail_url) time.sleep(random.randint(3,5))这段代码为什么只能输出最后一个网站的内容,应该怎么修改
时间: 2024-03-26 18:35:20 浏览: 81
Java_NIO-Selector.rar_java nio_selector
这段代码只输出最后一个网站的内容是因为在循环中只是将每个play输出了,并没有将其他的信息输出。可以将每个网站的信息放在一个字典中,再将这些字典放在一个列表中,最后输出整个列表即可。修改后的代码如下:
```
detail_selector = parsel.Selector(detail_html)
title = detail_selector.css('.b_crumb_cont *:nth-child(3)::text').get()
comment = detail_selector.css('.title white::text').get()
view_count = detail_selector.css('.view_count::text').get()
date = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.when > p > span.data::text').get()
days = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.howlong > p > span.data::text').get()
money = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.howmuch > p > span.data::text').get()
character = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.who > p > span.data::text').get()
play_list = detail_selector.css('#js_mainleft > div.b_foreword > ul > li.f_item.how > p > span.data > span::text').getall()
result_list = []
for play in play_list:
result_dict = {}
result_dict['title'] = title
result_dict['comment'] = comment
result_dict['view_count'] = view_count
result_dict['date'] = date
result_dict['days'] = days
result_dict['money'] = money
result_dict['character'] = character
result_dict['play'] = play
result_dict['detail_url'] = detail_url
result_list.append(result_dict)
print(result_dict)
time.sleep(random.randint(3,5))
```
这样就能够输出每个网站的所有信息了。
阅读全文