gpt4 book ai didi

python - Scrapy爬取的URL如何存储?

转载 作者:行者123 更新时间:2023-11-28 16:44:43 29 4
gpt4 key购买 nike

我有一个网络爬虫,可以在网页上抓取新闻报道。

我知道如何使用 XpathSelector 从页面元素中抓取某些信息。

但是我似乎无法弄清楚如何存储刚刚抓取的页面的 URL。

class spidey(CrawlSpider):
name = 'spidey'
start_urls = ['http://nytimes.com'] # urls from which the spider will start crawling
rules = [Rule(SgmlLinkExtractor(allow=[r'page/\d+']), follow=True),
# r'page/\d+' : regular expression for http://nytimes.com/page/X URLs
Rule(SgmlLinkExtractor(allow=[r'\d{4}/\d{2}/\w+']), callback='parse_articles')]
# r'\d{4}/\d{2}/\w+' : regular expression for http://nytimes.com/YYYY/MM/title URLs

我想存储通过这些规则的每个链接。

我需要向 parse_articles 添加什么以将链接存储在我的项目中?

def parse_articles(self, response):
item = SpideyItem()
item['link'] = ???
return item

最佳答案

response.url 就是您要查找的内容。

参见 docs在响应对象上并检查 this简单的例子。

关于python - Scrapy爬取的URL如何存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15106029/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com