selector = parsel.Selector(html_data) lis = selector.css('.list_item h2 a::attr(href)').getall() details = [] for li in lis: detail_url = 'https://travel.qunar.com' + li details.append(detail_url) # 输出每个网站的链接 for detail_url in details: print(detail_url) #解析数据 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-30 14:33:52 浏览: 120
Java_NIO-Selector.rar_java nio_selector
这段代码只能输出最后一个网站的内容是因为在循环结束后,变量 `detail_html` 的值仍然是最后一个网站的内容,而在循环内部对该变量的赋值并没有被使用。
要修复这个问题,你可以将循环内部的代码封装成一个函数,然后在循环内部调用该函数,将每个网站的内容都进行解析和打印输出。具体代码实现如下:
```python
selector = parsel.Selector(html_data)
lis = selector.css('.list_item h2 a::attr(href)').getall()
details = []
for li in lis:
detail_url = 'https://travel.qunar.com' + li
details.append(detail_url)
# 定义解析详情页的函数
def parse_detail(detail_url):
detail_html = requests.get(detail_url).text
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))
# 循环遍历每个网站的链接,并调用函数解析和输出详情页内容
for detail_url in details:
parse_detail(detail_url)
```
这样就可以输出每个网站的内容了。
阅读全文