我有一个网络爬虫,抓取新闻故事在网页上。
我知道如何使用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发布于 2013-02-27 15:05:16
response.url就是你要找的东西。
参见docs on response object和查看this simple example。
https://stackoverflow.com/questions/15106029
复制相似问题