在抓取给定网站时,我在打印重定向的网址(301重定向后的新网址)时遇到了一些问题.我的想法是只打印它们而不是刮掉它们.我目前的一段代码是:
import scrapy
import os
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class MySpider(CrawlSpider):
name = 'rust'
allowed_domains = ['example.com']
start_urls = ['http://example.com']
rules = (
# Extract links matching 'category.php' (but not matching 'subsection.php')
# and follow links from them (since no callback means follow=True by default).
# Extract links matching 'item.php' and parse them with the spider's method parse_item
Rule(LinkExtractor(), callback='parse_item', follow=True),
)
def parse_item(self, response):
#if response.status == 301:
print response.url
Run Code Online (Sandbox Code Playgroud)
但是,这不会打印重定向的URL.任何帮助将不胜感激.
谢谢.
我无法找到该问题的答案.scrapy蜘蛛退出后如何执行python代码:
我在解析响应的函数中做了以下内容(def parse_item(self,response):):self.my_function()我定义了my_function(),但问题是它仍然在蜘蛛的循环中.我的主要想法是使用收集的数据在蜘蛛循环外的函数中执行给定代码.谢谢.