(我是新来的刮痧)
假设有一个站点https://example.com
,我想将其删除
它的结构如下:
<body>
<ul>
<li>
title_foo
<a href="https://example.com/title_foo">a desription</a>
</li>
<li>
title_bar
<a href="https://example.com/title_bar">an another desription</a>
</li>
</ul>
</body>
在<a>
链接之后,我可以在创建项目之前获得所需的描述,并将它们发送到我的管道,管道将把它们存储到我的数据库中
例如,假设当我遵循https://example.com/title_foo时,我将该描述检索到
<div class="a-descrption">
a description
</div>
在{
class MyItem(scrapy.Item):
title = scrapy.Field()
description = scrapy.Field()
我的蜘蛛应该是这样的:
import scrapy
from scrapy_project.items import MyItem
class MySpider(scrapy.Spider):
name = "my_spider"
def start_requests(self):
urls = [
'https://example.com',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
for li in response.xpath('//li/text()').getall()
yield {"title": li }
至少,我希望,我没有测试它,如果有什么问题,请纠正我
这里有一种“等待”机制。我的意思是,我需要告诉scrapy在生成我的项目之前等待链接被访问。这似乎是一个常见的问题,我确信有些实践是为了解决这个问题而存在的,但无法找到解决方法
你好!
在您的示例中,刮取的唯一字段是
title
,因此我不完全确定,但听起来您想在https://example.com刮取title
,请求一个详细信息页面(如https://example.com/title_foo)在那里刮取描述,然后yield
同时使用description
和title
刮取项目如果是这种情况,这类问题的常见解决方案是使用cb_kwargs或meta。(如果您使用的是Scrapy v1.7+,建议使用cb_kwargs)
cb_kwargs
允许您将任意数据传递到请求的回调函数中。需要注意的是,数据是作为关键字参数传递的。例如:在这里,存储在
title
中的第一页上的数据“伴随着”对细节页的请求,当调用回调函数时title
作为参数接收,因此您可以从函数访问它相关问题 更多 >
编程相关推荐