Python 刮擦运行顺序,在其之前打印';指定的
所以问题是运行顺序,它基本上是最后运行函数Python 刮擦运行顺序,在其之前打印';指定的,python,scrapy,web-crawler,Python,Scrapy,Web Crawler,所以问题是运行顺序,它基本上是最后运行函数 import scrapy class uppspider(scrapy.Spider): start_urls = ['something.com'] def parse(self, response): return scrapy.FormRequest.from_response( response, formdata={'login': '',
import scrapy
class uppspider(scrapy.Spider):
start_urls = ['something.com']
def parse(self, response):
return scrapy.FormRequest.from_response(
response,
formdata={'login': '', 'Password': ''},
callback=self.after_login
)
def after_login(self, response):
#check login succeed before going on
return Request(url="", callback=self.ret)
def ret(self, response):
#scraping
yield scrapy.Request(callback=self.parse_tastypage)
def parse_tastypage(self, response):
item = uppItem()
er = response.status
self = list()
self.append(er)
#scraping
yield item
print "whatever i print here, prints before the spider"
mylist = list()
parse_tastypage(mylist, 0)
print (mylist)
所以,如果我想打印函数中指定的变量。它不工作,因为它在函数中指定之前被打印 @Umair我基本上想打印在该函数之外的最后一个函数中分配的变量(er)。它不起作用。这个parse_tastypage(self):应该是def parse_tastypage(self,response):并且parse_tastypage()需要有两个参数。好的话,你可以更改它,但你什么都没做。你只要写我一直写的东西Tenbro看看“自我”我加了一句。这是访问函数内部类变量的方法
import logging
class uppspider(scrapy.Spider):
mylist = list()
def parse_tastypage(self):
# access the above declared list like this
self.mylist = ['some data']
parse_tastypage()
logging.info(mylist) # this will print ['some data']