Scrapy - 不迭代
Scrapy - Not Iterating Through
我是 scrapy 的新手,正在根据文档/教程进行试验。我正在编写一个简单的机器人来抓取黑客新闻,最终只想提取一定数量的故事。我已经到了我的循环只为第 1 页和第 2 页上的所有结果填充相同的故事标题 / link 的地步。我如何真正让它检查每个故事而不是只检查每个页面上的第一个故事?代码如下:
import scrapy
class ArticlesSpider(scrapy.Spider):
name = 'articles'
start_urls = [
'https://news.ycombinator.com',
'https://news.ycombinator.com/news?p=2'
]
def parse(self, response):
link = response.css('tr.athing')
for website in link:
yield {
'title': link.css('tr.athing td.title a.storylink::text').get(),
'link': link.css('tr.athing td.title a::attr(href)').get()
}
我的控制台中的输出是标题和 link 的字典形式,但每页完全相同(30 次)。我做错了什么?
内部循环你需要使用 website.css..
而不是 link.css...
:
应该喜欢
def parse(self, response):
link = response.css('tr.athing')
for website in link:
yield {
'title': website.css('td.title a.storylink::text').get(),
'link': website.css('td.title a::attr(href)').get()
}
我是 scrapy 的新手,正在根据文档/教程进行试验。我正在编写一个简单的机器人来抓取黑客新闻,最终只想提取一定数量的故事。我已经到了我的循环只为第 1 页和第 2 页上的所有结果填充相同的故事标题 / link 的地步。我如何真正让它检查每个故事而不是只检查每个页面上的第一个故事?代码如下:
import scrapy
class ArticlesSpider(scrapy.Spider):
name = 'articles'
start_urls = [
'https://news.ycombinator.com',
'https://news.ycombinator.com/news?p=2'
]
def parse(self, response):
link = response.css('tr.athing')
for website in link:
yield {
'title': link.css('tr.athing td.title a.storylink::text').get(),
'link': link.css('tr.athing td.title a::attr(href)').get()
}
我的控制台中的输出是标题和 link 的字典形式,但每页完全相同(30 次)。我做错了什么?
内部循环你需要使用 website.css..
而不是 link.css...
:
应该喜欢
def parse(self, response):
link = response.css('tr.athing')
for website in link:
yield {
'title': website.css('td.title a.storylink::text').get(),
'link': website.css('td.title a::attr(href)').get()
}