在我的以下文件中
Reddit.py
,它有一只蜘蛛:
import scrapy
class RedditSpider(scrapy.Spider):
name = 'Reddit'
allowed_domains = ['reddit.com']
start_urls = ['https://old.reddit.com']
def parse(self, response):
for link in response.css('li.first a.comments::attr(href)').extract():
yield scrapy.Request(url=response.urljoin(link), callback=self.parse_topics)
def parse_topics(self, response):
topics = {}
topics["title"] = response.css('a.title::text').extract_first()
topics["author"] = response.css('p.tagline a.author::text').extract_first()
if response.css('div.score.likes::attr(title)').extract_first() is not None:
topics["score"] = response.css('div.score.likes::attr(title)').extract_first()
else:
topics["score"] = "0"
if int(topics["score"]) > 10000:
author_url = response.css('p.tagline a.author::attr(href)').extract_first()
yield scrapy.Request(url=response.urljoin(author_url), callback=self.parse_user, meta={'topics': topics})
else:
yield topics
def parse_user(self, response):
topics = response.meta.get('topics')
users = {}
users["name"] = topics["author"]
users["karma"] = response.css('span.karma::text').extract_first()
yield users
yield topics
它从主页获取所有的URL
old.reddit
,然后抓取每个URL的
标题
,请
作者
和
分数
.
我添加的是第二部分,它检查
分数
高于
10000个
如果是的话,蜘蛛就会去
用户
的页面和刮擦他的
因果报应
从它开始。
我知道我可以刮
因果报应
来自
话题
的页面,但我想这样做,因为
用户
我擦掉的那一页
话题
的页面。
我要做的是出口
topics
包含以下内容的列表
title, author, score
变成
JSON
文件名为
topics.json
,那么如果
话题
的分数高于
10000个
导出
users
包含以下内容的列表
name, karma
变成
JSON公司
文件名为
users.json
.
我只知道如何使用
command-line
属于
scrapy runspider Reddit.py -o Reddit.json
将所有列表导出到一个
JSON公司
文件名为
Reddit
但在这样的糟糕结构中
[
{"name": "Username", "karma": "00000"},
{"title": "ExampleTitle1", "author": "Username", "score": "11000"},
{"name": "Username2", "karma": "00000"},
{"title": "ExampleTitle2", "author": "Username2", "score": "12000"},
{"name": "Username3", "karma": "00000"},
{"title": "ExampleTitle3", "author": "Username3", "score": "13000"},
{"title": "ExampleTitle4", "author": "Username4", "score": "9000"},
....
]
我完全不知道
碎屑
的
Item Pipeline
也没有
Item Exporters
&安培;
Feed Exporters
关于如何在我的蜘蛛上实现它们,或者如何全面地使用它们,我试图从文档中了解它,但似乎我不知道如何在我的蜘蛛中使用它。
我想要的最终结果是两个文件:
主题.json
[
{"title": "ExampleTitle1", "author": "Username", "score": "11000"},
{"title": "ExampleTitle2", "author": "Username2", "score": "12000"},
{"title": "ExampleTitle3", "author": "Username3", "score": "13000"},
{"title": "ExampleTitle4", "author": "Username4", "score": "9000"},
....
]
用户.json
[
{"name": "Username", "karma": "00000"},
{"name": "Username2", "karma": "00000"},
{"name": "Username3", "karma": "00000"},
....
]
同时清除列表中的重复项。