**selector_kwargs
时间: 2023-10-04 14:11:10 浏览: 41
selector_kwargs 是一个用于传递参数给 selector 的字典。在聊天机器人中,selector 通常用于根据用户的输入选择回复的方式。selector_kwargs 可以用于向 selector 传递额外的参数,例如上下文信息、用户的历史输入等,以便更准确地选择回复。这些参数可能会因机器人的实现而异,但通常包括一个包含历史输入的列表、当前对话状态的字典、用户资料等。
相关问题
请帮我完成scrapy下面的代码,要求能够在comments_url提供的url下进行循环并进行爬取,以下是我的代码 def parse_detail(self, response, **kwargs): movie_item = kwargs['item'] sel = Selector(response) movie_item['directors'] = sel.xpath('//a[@rel="v:directedBy"]/text()').extract() movie_item['casts'] = sel.xpath('//a[@rel="v:starring"]/text()').extract() movie_item['types'] = sel.css('span[property="v:genre"]::text').extract() movie_item['country'] = sel.css('span[property="v:initialReleaseDate"]::text').extract() movie_item['movie_time'] = sel.css('span[property="v:runtime"]::text').extract() comments = movie_item['url'] comment = "/comments?start=0&limit=20&status=P&sort=new_score" comments_url = comments + comment
以下是代码,可以在comments_url提供的url下进行循环并进行爬取:
```
def parse_detail(self, response, **kwargs):
movie_item = kwargs['item']
sel = Selector(response)
movie_item['directors'] = sel.xpath('//a[@rel="v:directedBy"]/text()').extract()
movie_item['casts'] = sel.xpath('//a[@rel="v:starring"]/text()').extract()
movie_item['types'] = sel.css('span[property="v:genre"]::text').extract()
movie_item['country'] = sel.css('span[property="v:initialReleaseDate"]::text').extract()
movie_item['movie_time'] = sel.css('span[property="v:runtime"]::text').extract()
# 获取评论url
comments = movie_item['url']
comment = "/comments?start={}&limit=20&status=P&sort=new_score"
start = 0
while True:
comments_url = comments + comment.format(start)
yield scrapy.Request(comments_url, callback=self.parse_comments, meta={'item': movie_item})
start += 20
def parse_comments(self, response):
movie_item = response.meta['item']
sel = Selector(response)
comments = sel.css('div.comment-item')
for comment in comments:
commenter = comment.css('a[href^="https://www.douban.com/people/"]::text').extract_first()
rating = comment.css('span.comment-info>span.rating::attr(title)').extract_first()
content = comment.css('span.short::text').extract_first()
time = comment.css('span.comment-time::text').extract_first()
movie_item['comments'].append({'commenter': commenter, 'rating': rating, 'content': content, 'time': time})
yield movie_item
```
在`parse_detail`函数中,我们使用一个while循环不断获取评论的url,其中start从0开始,每次增加20,直到获取不到评论为止。然后,我们使用scrapy.Request发送请求,将获取到的评论url传递给`parse_comments`函数进行解析,并将电影信息传递给meta参数。在`parse_comments`函数中,我们使用css选择器获取评论信息,并将其添加到电影信息的comments字段中,最后将电影信息yield出去。
WARNING: E:\python_w\lib\site-packages\scrapy\selector\unified.py:82: UserWarning: Selector got both text and root, root is being ignored. super().init(text=text, type=st, root=root, **kwargs)这个警告影响scrapy使用吗?
这个警告通常不会影响 Scrapy 的使用,因为它只是一个警告,而不是一个错误。警告的原因可能是由于 Scrapy 的一些内部实现和第三方库之间的交互方式导致的。如果你遇到了这个警告,可以尝试忽略它,或者查看警告的代码行并尝试了解它的具体含义。如果这个警告对你的代码有实际影响,你可以考虑升级相关的库或者寻找其他解决方案。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)